Remove elements from array using spread

Solution 1:

No, you can't, because they're not next to each other. There's been some discussion of taking spread and rest syntax further, but even in those discussions, I don't think a series of discontiguous selections would be possible.

I think the closest you can get is to call out the first few specifically and then use rest syntax for everything after 'lion':

const arr = ['cat','dog','elephant','lion','tiger','mouse'];
const arr2 = [arr[1], arr[2], ...arr.slice(4)];
console.log(arr2);

...which I'm sure isn't what you wanted to do. :-)

Solution 2:

From what I understand you're looking for a function that has its argument defined using the spread syntax. Here is an example:

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

function remove(...toRemove){
   toRemove.forEach(item => {
      var index = arr.indexOf(item);
      if(index != -1){
          arr.splice(index, 1);
      }
   })
}

remove('dog', 'lion'); // OR remove(...['dog', 'lion']);
console.log(arr);

This is actually changing the original array (it mutates it) you've mentioned that you're not looking for mutation but you've also mentioned this arr should get changed to...