2016-10-01 67 views
0

我需要知道如何從包含多個詞條目的數組中刪除單個詞條目。這裏是數組的一個例子:刪除數組中的單個詞條

輸入:

[ "Off the bloody wall", "who?", "That sounds familiar" , "lol", "what the hell" ] 

所需的輸出

[ "Off the bloody wall", "That sounds familiar" , "what the hell" ] 

我可以把一個多級解決方案,這一點,但也許有人在這裏知道快速的單行jQuery解決方案嗎?

回答

2

您可以使用Array.protype.filter刪除不通過謂詞函數的元素,並使用String.prototype.match來測試字符串是否匹配。在這種情況下,如果沒有空間,我們會過濾掉該屬性。

const arr = [ "Off the bloody wall", "who?", "That sounds familiar" , "lol", "what the hell" ] 
 

 
console.log(
 
    arr.filter(item => item.match(' ')) 
 
)