首页 > 代码库 > Falsy Bouncer

Falsy Bouncer

 

真假美猴王!

删除数组中的所有假值。

在JavaScript中,假值有falsenull0""undefinedNaN

 

/*
console.log(Boolean(0));        // false
console.log(Boolean(-0));       // false
console.log(Boolean(‘0‘));      // true
console.log(Boolean(false));    // false
console.log(Boolean(‘false‘));  // true
console.log(Boolean(true));     // true
console.log(Boolean(‘true‘));   // true
console.log(Boolean(null));     // false
console.log(Boolean(NaN));      // false
console.log(Boolean(undefined));// false
console.log(Boolean(‘‘));       // false
console.log(Boolean(1));        // true
console.log(Boolean(-1));       // true
console.log(Boolean(3));        // true
console.log(Boolean("w3cplus"));// true
*/
function bouncer(arr) {
return arr.filter( function( value ){
     return value;
 });
}
bouncer([‘sd‘, "ate", "a", false, 9]);

 

Falsy Bouncer