2

I'm trying to remove all falsy values from an array.

I know this might not be the optimal solution for this problem but considering that I want to use the switch, is there a way to remove the NaN?

function bouncer(arr) {
    arr=arr.filter(removeFalsy);
  return arr;
}

function removeFalsy(val){
    switch(val){
        case false: return false;
        case null: return false;
        case 0: return false;
        case "" :return false;
        case undefined: return false;
        case NaN: return false; // <=== NaN 
        default: return true;
    }
}

bouncer([false, null, 0, NaN, undefined, ""]);
//result is: [ NaN ]

bouncer([1, null, NaN, 2, undefined]);
//result is: [ 1, NaN, 2 ]
jruivo
  • 447
  • 1
  • 8
  • 22
  • `NaN == NaN //false` Use [Number.isNaN](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Number/isNaN) – VLAZ Aug 17 '16 at 21:12
  • Anyway, since `NaN` is a falsy value, you could just filter out, all falsy values – VLAZ Aug 17 '16 at 21:13
  • Possible duplicates: [Filter null from an array in JavaScript](https://stackoverflow.com/q/41346902) and [How to filter() out NaN, null, 0, false in an array (JS)](https://stackoverflow.com/q/31925323). – Henke Mar 17 '21 at 10:37

3 Answers3

7

Sometimes you just need to keep it simple:

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

console.log(bouncer([false, null, 0, NaN, undefined, ""]));
console.log(bouncer([1, null, NaN, 2, undefined]));
Amit
  • 45,440
  • 9
  • 78
  • 110
4

NaN is not equal to anything, therefore you need to use Number.isNaN. As explained in the comments, the global isNaN is not a good idea.

Thanks for pointing that out.

For an example see how-do-you-have-a-nan-case-in-a-switch-statement

Community
  • 1
  • 1
ASDFGerte
  • 4,695
  • 6
  • 16
  • 33
  • @Vld Thanks for the hint. I again did the mistake of assuming core functions would behave intuitively. Changed post accordingly. – ASDFGerte Aug 17 '16 at 21:30
  • 2
    @ASDFGerte I see. I understand the confusion, but `isNaN` should be one of those "known" problems with JS. I've removed my comment but I'll add the important part from it: `isNaN` is very badly named - it doesn't check if the argument passed in is `NaN` (what you'd guess from the name) but whether or not the argument _would be_ `NaN` if converted to a number. Its actual name should be `isThisUsableInAMathematicalExpression`. I guess that's a bit unwieldy, though. Some examples `isNaN("") //false` `isNaN("a") //true` `isNaN([1])//false` `isNaN(["a"]) //true` logic being `+""//0` but `+"a"//NaN` – VLAZ Aug 17 '16 at 21:36
0

The filter() method creates a new array with all elements that pass the test implemented by the provided function.

Since false, null, 0, "", undefined and NaN are all Falsy values in JavaScript therefore they will return false when tested.

function bouncer(arr) {
  var array = arr.filter(function(val){
    return val;
  });
  return array;
}

Only values which do not return false will be added to the array.

This question is same as this question.

Community
  • 1
  • 1
Ravi Kumar Seth
  • 154
  • 2
  • 14