Sorted Union: Using Set() does not work

Tell us what’s happening:

I am trying to use the new ES6 Set() to filter the array but it doesn’t seem to work with variables. If I enter the array manually it works as expected.

Your code so far


function uniteUnique(...arr) {
console.log(arr)

let uniq = [...new Set(arr)]

console.log(uniq)

  return uniq;
}

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

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/sorted-union/

Using REST (...) operator gives us an array of arguments in an array. So in your example, it would be:

[ [1, 3, 2], [5, 2, 1, 4], [2, 1] ]

Notice the wrapper array! When you say manually it works, you must be skipping the wrapper array that gets added automatically.

[...new Set([1, 3, 2], [5, 2, 1, 4], [2, 1])]

The above works. But if you try the same with the wrapping array, it won’t work.

SPREAD the arr variable too, and it will work.

PS: Rest and Spread operators are written the same way. Read more about them here: https://scotch.io/bar-talk/javascripts-three-dots-spread-vs-rest-operators543