真假美猴王!
删除数组中的所有假值。
在JavaScript中,假值有false
、null
、0
、""
、undefined
和 NaN
。
1 /*思路 2 利用布尔值构造filter的测试函数,判断数组中不为false的元素; 3 */ 4 5 6 function checktrue(ele){ 7 return Boolean(ele)!==false; 8 } 9 function bouncer(arr) { 10 return arr.filter(checktrue); 11 } 12 13 bouncer([7, "ate", "", false, 9]);
原文:http://www.cnblogs.com/zhouhelong/p/5912205.html