2015-11-04 36 views

回答

2

filter(Boolean)將只保留數組中的truthy值。

filter需要一個回調函數,通過提供Boolean作爲參考,它會被稱爲Boolean(e)爲陣列中的每個元件e和將返回到filter的操作的結果。

如果返回的值是true,元素e將保留在數組中,否則它不包含在數組中。

var arr = [0, 'A', true, false, 'tushar', '', undefined, null, 'Say My Name']; 
 
arr = arr.filter(Boolean); 
 
console.log(arr); // ["A", true, "tushar", "Say My Name"]


在代碼

var adds = emailString.split(/;+/).filter(Boolean); 

我的猜測是字符串emailString包含由01分隔值分號可以出現多次。

> str = '[email protected];;;;[email protected];;;;[email protected];' 
> str.split(/;+/) 
< ["[email protected]", "[email protected]", "[email protected]", ""] 

> str.split(/;+/).filter(Boolean) 
< ["[email protected]", "[email protected]", "[email protected]"] 

這裏split上這將返回["[email protected]", "[email protected]", "[email protected]", ""]

+0

所以我的新的'emailString.split(「;」)。過濾器(布爾);'會做同樣的,並過濾掉造成連續的分號空字符串。謝謝。 – ProfK

相關問題