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]);

 

function bouncer(arr) {
  // Don't show a false ID to this bouncer.
  return arr.filter(Boolean);
}

function bouncer(arr) {
  // Don't show a false ID to this bouncer.
  return arr.filter(function(item,index,array){
    return Boolean(item);
  });
}

 

posted @ 2016-11-24 20:45  兴趣使然的椰子  阅读(266)  评论(0编辑  收藏  举报