从数组中删除所有假值

106

我想从数组中删除所有假值。在JavaScript中,假值包括false、null、0、""(空字符串)、undefined和NaN。

function bouncer(arr) {
 arr = arr.filter(function (n) { 
    return (n !== undefined && n !== null && n !== false && n !== 0 && n !== "" && isNaN()!=NaN); });
  return arr;
}

bouncer([7, "ate", "", false, 9, NaN], "");

除了NaN测试用例外,以上所有内容都已满足。请问有人能帮我在数组中检查是否包含NaN吗?

26个回答

3

只需要进行两次取反就可以"强制"转换为布尔值。!NaN === true => !!NaN === false

    const truthy = arr.filter(o => !!o)

3

感谢以上所有的工作答案。以下是三种解决该问题的方法。第三个解决方案采用了您@Vignesh的方法来解决问题。

1. 
function bouncer(arr) {
  return arr.filter( function( val ){
        return val;
    });
}

2. 
function bouncer(arr) {
return arr.filter(Boolean);
}
3.
  function bouncer(arr) {
  return arr.filter(function(val){
      return val !== false && val !== "" && !(Number.isNaN(val)) && val !== 
undefined && val !== 0 && val !== null;
    });
 }

2

假值

  • false(假)
  • 零(0,-0)
  • 空字符串(“”,‘ ‘,` `)
  • BigIntZero(0,0x0n)
  • null(空)
  • undefined(未定义)
  • NaN(非数字)

const values = [false,'false','js', true, 0 , [],[1,2,3], 1, 'b', {}, 
'', NaN, undefined, null, -5,1n,0n,0x1n,0x0n];

console.log(values.filter((value)=> !!value));
console.log(values.filter((value) => value ));
console.log(values.filter((value)=> Boolean(value)));
console.log(values.filter(Boolean));

//=> [ 'false', 'js', true, [], [ 1, 2, 3 ], 1, 'b', {}, -5, 1n, 1n ]

// note: empty collections are not falsy like in python (empty array)
//note: will syntax error for 1x0n, 'false' is string here
const values = [false,'false','js', true, 0 , [],[1,2,3], 1, 'b', {}, '', NaN, undefined, null, -5,1n,0n,0x1n,0x0n];
//=> [ 'false', 'js', true, [], [ 1, 2, 3 ], 1, 'b', {}, -5, 1n, 1n ]
//=> [ 'false', 'js', true, [], [ 1, 2, 3 ], 1, 'b', {}, -5, null, null ] // BigInt not supported compilers

// double not 
// first not makes it as boolean opposite values, then  second not give its inversion
console.log(values.filter(
  (value)=> !!value
));

// Auto Coercion 
// values are self identified as falsy or not
console.log(values.filter(
  value => value
));

// Boolean type conversion
// get values as parem and return boolean as falsy or not
console.log(values.filter(
  (value)=> Boolean(value)
));

// Boolean constructor
// This works because Boolean itself is a function, and the arguments filter supplies are passed directly to it
console.log(values.filter(
  Boolean
));

有关详细说明,请参考:samanthaming网站


2

这是我的想法...

function bouncer(arr) {
  // Don't show a false ID to this bouncer.
  var result = [];
  
    function isGood(obj){
      if(!Boolean(obj)){
        return false;
      } else {
        return true;
      }
    }
    
    for (var i=0; i < arr.length; i++){
      if (isGood(arr[i]) === true){
        result.push(arr[i]);
      }
    }
  return result;
}

console.log(bouncer([7, "ate", "", false, 9]));


2

尝试使用过滤器和布尔值:

最初的回答

let array = [7,"ate","",false,9];
array.filter((values) => {return Boolean(values) === true })

2

1
function falsy(value) {
      if (value) {
        return value;
      }
    }

    function bouncer(arr) {
      var filter = arr.filter(falsy);
      return filter;
    }

    bouncer([7, "ate", "", false, 9]);

1
function bouncer(arr) {  
  var result = []; 
   for (var i = 0; i < arr.length; i++) {
     if (arr[i]) {
      result.push(arr[i]);
     }
   }
  return result;
 }

 bouncer([7, "ate", "", false, 9]);

1
使用.filter
myArray.filter(val => Boolean(val));

1
这应该是你正在寻找的内容:
let array = [7, 'ate', '', false, 9, NaN];

function removeFalsyItems(array) {
   // Your result
   let filter = array.filter(Boolean);

   // Empty the array
   array.splice(0, array.length);

   // Push all items from the result to our array
   Array.prototype.push.apply(array, filter);

   return array
}

removeFalsyItems(array) // => [7, 'ate', 9], funny joke btw...

你还可以通过 Array.prototype 将其转换为数组方法,以使其更加方便,例如:[1, 0, 1].removeFalsyItems() // => [1, 1] - Lapys

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接