So here’s a very cool and tricky way to remove duplicates from an array with just one line of code.
The Set object’s constructor will take in any iterable object, which includes arrays. So we can give a Set an array, and it’ll load in the elements and any duplicates won’t get loaded. We do that like this:
Now that we have a Set with the unique items we want, we just need to turn that back into an array. Here the Array.from method comes to the rescue. This static method will take any array-like or iterable object, and create a new array from it. So when we pass in our Set to Array.from, it’ll create a new array with the elements of the Set.
So combining these, in a single line of code, we can remove duplicates from an Array with nice, concise code.