Remove all falsy values from an array

2020-01-27 03:03发布

I would like to remove all falsy values from an array. Falsy values in JavaScript are false, null, 0, "", undefined, and 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], "");

The above is getting satisfied for all except the NaN test case. Can someone help me check in the array whether it contains NaN or not?

22条回答
欢心
2楼-- · 2020-01-27 03:13

Since you want to get rid of "falsy" values, just let JavaScript do its thing:

function bouncer(arr) {
  return arr.filter(function(v) { return !!v; });
}

The double-application of the ! operator will make the filter callback return true when the value is "truthy" and false when it's "falsy".

(Your code is calling isNaN() but not passing it a value; that's why that test didn't work for you. The isNaN() function returns true if its parameter, when coerced to a number, is NaN, and false otherwise.)

edit — note that

function bouncer(arr) {
  return arr.filter(Boolean);
}

would work too as LoremIpsum notes in another answer, because the built-in Boolean constructor does pretty much the exact same thing as !!.

查看更多
SAY GOODBYE
3楼-- · 2020-01-27 03:14

Using this simple filter will do:

array.filter(Boolean)

You can read more about Boolean here

查看更多
乱世女痞
4楼-- · 2020-01-27 03:14

I know this can be done using the arr.filter() method. But I prefer using the Boolean() function. Is clearer to me. Here's how I did it, although a little longer:

function bouncer(arr) {
// Don't show a false ID to this bouncer.

    var falsy;
    var trueArr = [];

    for (i = 0; i < arr.length; i++) {

        falsy =  Boolean(arr[i]);

        if (falsy === true) {

        trueArr.push(arr[i]);

        }

    }

    return trueArr;
}

bouncer([7, "ate", "", false, 9]);
// returns a new array that is filtered accordingly.
查看更多
The star\"
5楼-- · 2020-01-27 03:14

I think a better deal this way

   function bouncer(arr) {
        arr = arr.filter(function(item) {
            return item;
        return arr;

    bouncer([7, "ate", "", false, 9, NaN, undefined, 0]);
查看更多
放荡不羁爱自由
6楼-- · 2020-01-27 03:17

This should be what you are looking for:

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...
查看更多
甜甜的少女心
7楼-- · 2020-01-27 03:18

This is my idea...

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

查看更多
登录 后发表回答