0

My sample array is below, I want to remove key 1 which is having "" value.

var a=[
       0: "349,602,317,805,887,810,863,657,665,865,102,624,659,636",
       1: ""
      ]

When i was browsing i found solution like use $.each() and keep condition. But i want to filter in single shot using jquery. Any function is there !!!!

2
  • It should be {object} notation, not [array]. Commented Feb 13, 2015 at 13:36
  • Or rather : this notation is invalid. Arrays do not take indexes. You mixed up an object and an array to get to this. Commented Feb 13, 2015 at 13:43

3 Answers 3

3
a.filter( Boolean );

will do the trick here! Since Array.prototype.filter is a standard Array method you don't need any 3rd party library.

What happens is, we call the .filter() method with the Boolean constructor method, which, only returns a truthy value if the value that gets passed in really is truthy for Javascript. Excludes:

  • Empty String ("")
  • 0
  • false
  • undefined
  • NaN
  • null
Sign up to request clarification or add additional context in comments.

4 Comments

can i know hot boolean is filtering "" pls
I actually just realized that you code is not valid. Are you sure you posted the correct code ?
you should update the question with the actual input code data, otherwise this gets very confusing.
It works on arrays, not objects (the OP's notation is invalid in the original post). +1 for the very elegant solution.
0

you use use something like this

var index = a.indexOf("");
if(index >=0)
 a.splice(index,1);

Comments

0

you can use the delete- operator. Its not the index.

delete a[key];

7 Comments

Yeah but the "" is not necessarily in position 1... that would be too easy :)
you can just set a key . the 1 isnt the index, its a key. @JeremyThille
Still, to find the ones with value "", you would need a loop
Yes, but in his question he wanted to remove the key. i thought it isnt depending on the value.
" " its not a key. You have to take the keys from the object. Like in this case 0 and 1
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.