真假美猴王!javascript
删除数组中的全部假值。java
在JavaScript中,假值有false、null、0、""、undefined 和 NaN。数组
咱们能够用 Boolean()
函数进行类型转换。若是它的参数是 0、-0、null、undefined、false、NaN、"",生成的Boolean对象的值会为false,也就是题目中说的“假值”。 ui
function bouncer(arr) { // Don't show a false ID to this bouncer. return arr.filter(function(item,index,array){ return Boolean(item); }); }
function bouncer(arr) { // Don't show a false ID to this bouncer. return arr.filter(Boolean); }
bouncer([7, "ate", "", false, 9])
应该返回 [7, "ate", 9]. this
bouncer(["a", "b", "c"])
应该返回 ["a", "b", "c"]. spa
bouncer([false, null, 0, NaN, undefined, ""])
应该返回 []. code
bouncer([1, null, NaN, 2, undefined])
应该返回 [1, 2]. 对象