Intermediate Algorithm Scripting: Sorted Union_Question about the Spread Operator

Hi. I had a question about why the spread operator [...arr] can’t be used in place of [...arguments].

Here is the code that I originally had that didn’t work because I used [...arr] instead of [...arguments].

function uniteUnique(arr) {
  let newArr = [];
  let fullArr = [...arr];
  console.log(newArr);
  for (let i = 0; i < fullArr.length; i++) {
    for (let j = 0; j < fullArr[i].length; j++) {
      if (newArr.indexOf(fullArr[i][j]) === -1) {
        newArr.push(fullArr[i][j]);
        
      }
    }
  }
  return newArr;
}

uniteUnique([1, 3, 2], [5, 2, 1, 4], [2, 1]);

There are not the same because the arguments object takes all the passed arguments and stores them, regardless of whether the number of parameters is less.

When calling uniteUnique, there isn’t an outer array to take all the passed arrays in one call. Consequently, the arr parameter takes only the first array [1,3,2] and misses the others. If you include an outer array, that will take all the subarrays, and the results from […arr] and […arguments] will be the same.

I see. I think I understand now.