Seek and Destroy - Arguments object can be handled as array?

Seek and Destroy - Arguments object can be handled as array?
0

#1

Hi all,

Below my solution. It is actually working but I do not understand how arguments could be handled as an array (length method) within the function.

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

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

If I create a similar object and try to get the length of it I get undefined

> myObj =  { '0': 1, '1': 1, '2': 2, '3': 3, '4': 4 }
{ '0': 1, '1': 1, '2': 2, '3': 3, '4': 4 }
> myObj.length
undefined

Any explanation is very welcome.

Regards,


#2

Literal objects like the myObj you declared do not have a length property.

The arguments object is a special “array-like” object, which is why it has a length property.

FYI - You could have use the rest syntax ... for the arguments like:

function destroyer(arr, ...args) {

Then, args would have contained only the arguments after the first array argument represented by arr.


#3

Hi @RandellDawson,

Understood.
Thanks for the super quick answer!

Regards,