簡體   English   中英

僅使用單詞從數組中刪除項目

[英]Remove Item from Array using only word

我想知道如何僅使用世界而不是數組 [0] 等從數組中刪除項目。

let input = ['a', 'b', 'c'];
let output = input.filter(item => item !== 'b'); //["a", "c"]

您可以為此使用splice方法並使用indexOf獲取元素位置

這將僅刪除第一個找到的元素

 let testArray = ['1','2','3','4']; let blackWorld = '2' if(testArray.includes(blackWorld)) testArray.splice(testArray.indexOf(blackWorld),1) console.log(testArray) // [ '1', '3', '4' ]

或使用filter方法,因此如果它們等於1 blackworld 的值,它將刪除所有元素

 let testArray = ['1','2','3','4','2']; let blackWorld = "2" let newArray = testArray.filter(item => item !== blackWorld) console.log(newArray) // [ '1', '3', '4' ]

對於一系列的黑世界

 let testArray = ['1','2','3','4']; let blackWorlds = ['1','2'] let newArray = testArray.filter(item => !blackWorlds.includes(item)) console.log(newArray) // [ '3', '4' ]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM