-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

Lokesh Agrawal
  • 3,269
  • 6
  • 28
  • 66

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,501
  • 4
  • 15
  • 34