Remove all falsy values from an array

后端 未结 22 3260
隐瞒了意图╮
隐瞒了意图╮ 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 08:04

    Using filter we can write

    function bouncer(arr) {
     return arr.filter(item => item);
    }
    bouncer([false, null, 0, NaN, undefined, ""]) // will return [].
    

提交回复
热议问题