compact使用Boolean过滤数组中的所有假值
我们知道JS中有一些假值: false, null, 0, “”, undefined, NaN,
怎样把数组中的假值快速过滤呢,可以使用Boolean构造函数来进行一次转换
compact =arr =>arr.filter
(
Boolean
)
const aaa = compact ([ 0 , 1 , false , 2 , '' , 3 , 'a' , 'e' * 23 , NaN , 's' , 34 ])
console.log(aaa);
打印结果: