真假美猴王!
删除数组中的所有假值。
在JavaScript中,假值有false
、null
、0
、""
、undefined
和 NaN
。
/* 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); }); }