0

I'm trying to Pass the values [1, 2, 3, 1, 2, 3] to be removed using the funcion "destroyer" and the values 2, 3 (or even more values ex: 1,3,5.) to be removed from the previous array. Always the first part is an array to remove from and followed by numbers to remove from the array

Here you have the code that I have to solve:

function destroyer(arr) {
  // Remove all the values
  return arr;
}

destroyer([1, 2, 3, 1, 2, 3], 2, 3);
Rafa
  • 105
  • 1
  • 8

2 Answers2

3

Try this approach. It uses the spread operator and the includes() function

... - is a spread operator

function destroyer(arr, ...items) {
    return arr.filter(i => !items.includes(i));
}

let arr = destroyer([1, 2, 3, 1, 2, 3], 2, 3);

console.log(arr);
Suren Srapyan
  • 57,890
  • 10
  • 97
  • 101
0

You can access all parameters passed to a function using arguments variable. Note, this is an array like object but not an array, so you will have to convert it to array. When you do that, your arr will be the first value as even that is a part of parameters. You can use .slice(1) to get all values from second element.

ES5

function destroyer(arr) {
  var args = [].slice.call(arguments,1);
  return arr.filter(function(val){
    return args.indexOf(val) < 0
  })
}

console.log(destroyer([1, 2, 3, 1, 2, 3], 2, 3));

ES6

function destroyer(arr) {
  var args = Array.from(arguments).slice(1);
  return arr.filter(x=>!args.includes(x));
}

console.log(destroyer([1, 2, 3, 1, 2, 3], 2, 3));
Community
  • 1
  • 1
Rajesh
  • 21,405
  • 5
  • 35
  • 66