过滤数组假值 (真假美猴王) 删除数组中的全部假值。 数组
在JavaScript中,假值有false、null、0、""、undefined 和 NaN。spa
注意:若是第一个参数不是布尔值,则会将其转换为布尔值。若是省略该参数,或者其值为 0
、-0
、null
、false
、NaN
、undefined
、或者空字符串(""
),则生成的 Boolean
对象的值为 false
。code
1 function bouncer(arr) { 2 3 return arr.filter(function(val){ 4 5 return Boolean(val); 6 }); 7 8 } 9 10 bouncer([7, "ate", "", false, 9]);
结果:对象
[7, "ate", 9]