Javascript Array filter() remove all falsey values

Description

Javascript Array filter() remove all falsey values

/*/* w w  w .  j  a v a2 s . co m*/
Remove all falsey values from an array.
Falsey values in javascript are false, null, 0, "", undefined, and NaN.
*/

function bouncer(arr) {
  return arr.filter(function(n){
    if (Boolean(n) !== false){
      return n;
    } 
  });
}

result = bouncer([false, null, 0, 1, "asdf", true]);

console.log(result);



PreviousNext

Related