真假美猴王!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]. 對象