-1

I have an array of animals arr = ['cat','dog','elephant','lion','tiger','mouse']

I want to write a function remove(['dog','lion']) which can remove the elements from arr, and returns a new array, what is the best and optimal solution?

example:

arr = ['cat','dog','elephant','lion','tiger','mouse'] 
remove(['cat', 'lion'])

arr should get changed to

arr = ['dog','elephant','tiger','mouse']

Note: No Mutations

ThinkGeek
  • 4,749
  • 13
  • 44
  • 91

1 Answers1

1

you can just use filter()

var arr = ['cat','dog','elephant','lion','tiger','mouse'];

var newArr = arr.filter(x => !['cat', 'lion'].includes(x))
console.log(newArr);
Dij
  • 9,761
  • 4
  • 18
  • 35