简体   繁体   中英

Remove Item from Array using only word

我想知道如何仅使用世界而不是数组 [0] 等从数组中删除项目。

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

You can use splice method for this and get element position with indexOf

This will remove only first finded element

 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' ]

Or use filter method, so it will remove all elements if they equal your value for 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' ]

For array of blackworlds

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM