63

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]);
Jason Aller
  • 3,541
  • 28
  • 38
  • 38
gshaineala
  • 485
  • 1
  • 4
  • 8
  • 1
    You should be aware that `.filter()` does not remove anything from the Array. It creates a new array with the items removed. –  Dec 27 '16 at 14:34
  • Possible duplicate of [How to filter() out NaN, null, 0, false in an array (JS)](https://stackoverflow.com/questions/31925323/how-to-filter-out-nan-null-0-false-in-an-array-js) – Kowser Aug 16 '17 at 22:27

5 Answers5

120

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.

Henke
  • 4,445
  • 3
  • 31
  • 44
tymeJV
  • 103,943
  • 14
  • 161
  • 157
  • 4
    And you don't need the `!!`. Just `x=>x` is enough, though @PranavCBalan's way doesn't require creation of a function. `.filter(Boolean)` –  Dec 27 '16 at 14:29
  • 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. – gshaineala Dec 27 '16 at 14:38
  • 2
    @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. – tymeJV Dec 27 '16 at 14:39
  • 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. – gshaineala Dec 27 '16 at 14:50
  • [Javascript comparison table](https://dorey.github.io/JavaScript-Equality-Table/) is quite handy when it comes to testing boolean equivalences. – Stphane Feb 22 '18 at 11:39
32

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]));
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
7
[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))
Hari Kishore
  • 2,201
  • 2
  • 19
  • 28
6

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]));
kukkuz
  • 41,512
  • 6
  • 59
  • 95
2

Use

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

console.log(bouncer([1,"", null, NaN, 2, undefined,4,5,6]));
Jyothi Babu Araja
  • 10,076
  • 3
  • 31
  • 38