-1

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

  • 3
    Does this answer your question? [Remove all elements contained in another array](https://stackoverflow.com/questions/19957348/remove-all-elements-contained-in-another-array) – Mohit Sharma Jul 18 '22 at 11:55

3 Answers3

0

You can use this, if I understand it correctly

arr.filter((value) => value != 'a' && value != 'b')

gives below output

['c', 'd', 'e']
Sam Dani
  • 114
  • 1
  • 6
  • This is exactly what the answers of the duplicate question already explain. There is no reason to explain it again on a new question. – Heretic Monkey Jul 18 '22 at 12:06
0

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

enter image description here

Art Bindu
  • 769
  • 4
  • 14
  • This is exactly what the answers of the duplicate question already explain. There is no reason to explain it again on a new question. – Heretic Monkey Jul 18 '22 at 12:05
0

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.