January 2020
Intermediate to advanced
548 pages
13h 36m
English
Set and WeakSet are native abstractions that allow us to store sequences of unique objects. This is in contrast to arrays, which give you no assurances as to the uniqueness of your values. Here's an illustration:
const foundNumbersArray = [1, 2, 3, 4, 3, 2, 1];const foundNumbersSet = new Set([1, 2, 3, 4, 3, 2, 1]);foundNumbersArray; // => [1, 2, 3, 4, 3, 2, 1]foundNumbersSet; // => Set{ 1, 2, 3, 4 }
As you can see, values given to a Set will always be ignored if they already exist in the Set.
Sets can be initialized by passing an iterable value to the constructor; for example, a string:
new Set('wooooow'); // => Set{ 'w', 'o' }
If you need to convert a Set into an array, you can most simply do this with the spread syntax (as ...
Read now
Unlock full access