i have an array that is
const arr = ["a","b","c","d","e"]
I need to exclude a,b that is I need. How to achieve this in fastest possible way
["c","d","e"]
i have an array that is
const arr = ["a","b","c","d","e"]
I need to exclude a,b that is I need. 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. 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.
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
const [one,two,three,...rest]=arr;
console.log(rest); // it prints d,e excluding a,b,c.