Filter null from an array in JavaScript

后端 未结 4 1544
予麋鹿
予麋鹿 2020-12-16 09:18

I have a task to remove false, null, 0, "", undefined, and NaN elements from an given array. I worked on a solution which removes all except null. Anyone can expla

4条回答
  •  庸人自扰
    2020-12-16 10:04

    Use

    function bouncer(arr) {
     return arr.filter(function(item){
       return !!item;
     });
    }
    
    console.log(bouncer([1,"", null, NaN, 2, undefined,4,5,6]));

提交回复
热议问题