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 ]