Why do I have to use array.prototype.slice.call?

Why do I have to use array.prototype.slice.call?
0

#1

Tell us what’s happening:

This is the task: “You will be provided with an initial array (the first argument in the destroyer function), followed by one or more arguments. Remove all elements from the initial array that are of the same value as these arguments.”

I think I’m not quite understanding the concept of the argument object.

So is arguments an array or an object?

And if it’s either one, why can’t I just filter the 0th argument(arr) with the following arguments(arguments[1], arguments[2] and so on…)?

I see that I have to use array.prototype.slice.call(arr), but I don’t understand why. Need some easy explanation please…

Your code so far

function destroyer(arr) {
  // Remove all the values
  
  var newArr=arr;
  for(var i=1;i<arguments.length;i++){
    newArr=newArr.filter(function(val){
      return val!=arguments[i];
    });
  }
  return newArr;
  
}

destroyer([1, 2, 3, 1, 2, 3], 2, 3);

Your browser information:

Your Browser User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36.

Link to the challenge:


#2

arguments isn’t really an array, so it has no direct access to the slice method. However, since it’s array-like (it has a length property and its contents can be accessed like an array), you can have the arguments object “borrow” the slice method (via Array.prototype.slice.call). The same is true for the other array methods.