69

I have a task to remove false, null, 0, "", undefined, and NaN elements from an given array. I worked on a solution which removes all except null. Anyone can explain why? Here's the code:

function bouncer(arr) {
var notAllowed = ["",false,null,0,undefined,NaN];
  for (i = 0; i < arr.length; i++){
      for (j=0; j<notAllowed.length;j++) {
         arr = arr.filter(function(val) {
               return val !== notAllowed[j];
              });
  }
 }
return arr;
}

bouncer([1,"", null, NaN, 2, undefined,4,5,6]);
2
  • 2
    You should be aware that .filter() does not remove anything from the Array. It creates a new array with the items removed. Commented Dec 27, 2016 at 14:34
  • Possible duplicate of How to filter() out NaN, null, 0, false in an array (JS) Commented Aug 16, 2017 at 22:27

5 Answers 5

129

Well, since all of your values are falsy, just do a !! (cast to boolean) check:

[1,"", null, NaN, 2, undefined,4,5,6].filter(x => !!x); //returns [1, 2, 4, 5, 6]

Edit: Apparently the cast isn't needed:

document.write([1,"", null, NaN, 2, undefined,4,5,6].filter(x => x));

And the code above removes "", null, undefined and NaN just fine.

Sign up to request clarification or add additional context in comments.

5 Comments

And you don't need the !!. Just x=>x is enough, though @PranavCBalan's way doesn't require creation of a function. .filter(Boolean)
Yes, your solution works.Thanks for that,but..i'm just starting to learn js so yea..it will help me to understand why my code doesn't work.
@squint -- Ahh didnt know that about the case, assumed I needed it. Also didnt know about the sweet Boolean rather than function creation... always learning, will make the edit.
Ok, i got it now. On the freecodecamp platform where I learn it doesnt filter null but if i run the code in the chrome console it does not filter NaN. Thank you all.
Javascript comparison table is quite handy when it comes to testing boolean equivalences.
36

It is a problem with NaN, because

NaN !== NaN

read more here: Testing against NaN.

For filtering the values, you could check for truthyness.

function bouncer(arr) {
    return arr.filter(Boolean);
}

console.log(bouncer([1, "", null, NaN, 2, undefined, 4, 5, 6]));

2 Comments

Thanks for the info. On the platform where i learn is doesnt filter null...yet in a debugger it doest filter NaN.
There is no problem with NaN in this case.
10
[1, 2, 0, null, "sdf", ""].filter(a=>a)
// filter non null value; remove 0, null and "" (empty quote)

console.log([1, 2, 0, null, "sdf", ""].filter(a=>a))

Comments

7

You can use Array.prototype.filter for truthy value check - see demo below:

function bouncer(array) {
  return array.filter(function(e) {
    return e;
  });
}

console.log(bouncer([1,"", null, NaN, 2, undefined,4,5,6]));

Comments

3

Use

function bouncer(arr) {
 return arr.filter(function(item){
   return !!item;
 });
}

console.log(bouncer([1,"", null, NaN, 2, undefined,4,5,6]));

Comments

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.