Remove all falsy values from an array

后端 未结 22 3072
隐瞒了意图╮
隐瞒了意图╮ 2020-11-28 07:21

I would like to remove all falsy values from an array. Falsy values in JavaScript are false, null, 0, \"\", undefined, and NaN.



        
相关标签:
22条回答
  • 2020-11-28 07:51

    You use isNaN() in wrong way. It should be something like following:

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

    }

    Also you can rewrite it:

    function bouncer( arr ){
        return arr.filter( function( value ){
            return value;
        });
    }
    
    0 讨论(0)
  • 2020-11-28 07:51

    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.
    
    0 讨论(0)
  • 2020-11-28 07:51

    This is another equivalent, but illustrative, solution:

    function bouncer( arr ){
        return arr.filter( function( value ){
            return value ? true : false;
        });
    }
    

    This code sample is illustrative because it indicates to a reader that the variable value will be evaluated as truthy or falsey, and the anonymous function will return a boolean, either true or false, mapping to the evaluation of value.

    For someone who is not familiar with this approach of removing values from an array based on their truthiness, or for someone who is not familiar with (or has not read the documentation on) the filter function, this example is the most concise that still conveys the behavior of the filter function.

    Of course, in your application you may opt for the more concise, yet less insightful, implementation:

    function bouncer( arr ){
        return arr.filter( function( value ){
            return value;
        });
    }
    
    0 讨论(0)
  • 2020-11-28 07:52
    truthyArray = arr.filter(el => el)
    

    ^ that's how you do it

    0 讨论(0)
  • 2020-11-28 07:52
    function removeFalsy(value){
    
      var val = Boolean(value);
      if(!val)
        return false;
      return true;
    }
    
    function bouncer(arr) {
    
      return arr.filter(removeFalsy);
    }
    
    bouncer([7, "ate", "", false, 9]);
    
    0 讨论(0)
  • 2020-11-28 07:53

    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 !!.

    0 讨论(0)
提交回复
热议问题