[英]remove sepcific items from array of strings javascript
i have an array that is我有一个数组
const arr = ["a","b","c","d","e"]
I need to exclude a,b that is I need.我需要排除我需要的 a,b。 How to achieve this in fastest possible way
如何以最快的方式实现这一目标
["c","d","e"]
You can use this, if I understand it correctly如果我理解正确,您可以使用它
arr.filter((value) => value != 'a' && value != 'b')
gives below output给出以下输出
['c', 'd', 'e']
Using array.filter() we can do it.使用 array.filter() 我们可以做到。 like:
喜欢:
arr = ["a","b","c","d","e"];
arr.filter(x => x !== 'a'); // output: ['b', 'c', 'd', 'e']
But if you want to remove a group of items then use another array and use validation with in filter()
function.但是,如果您想删除一组项目,请使用另一个数组并在
filter()
函数中使用验证。
Example:例子:
arr = ["a","b","c","d","e"];
removeItems = ['a', 'b'];
arr.filter(x => removeItems.indexOf(x) < 0); // ['c', 'd', 'e']
you can do it in 1 line using spread operator您可以使用扩展运算符在 1 行中完成
const [one,two,three,...rest]=arr;
console.log(rest); // it prints d,e excluding a,b,c.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.