May 2019
Beginner to intermediate
650 pages
14h 50m
English
Besides arrays, ES2015 also introduced two new data structures: Map, an associative array with key-value pairs easier to use than simple objects, and Set, which doesn't allow repeated values. Both can be transformed to and from arrays.
You can create a new Set object using:
const set = new Set();
And add elements to it using:
set.add(5);set.add(7);
If you try to add elements that already exist, they won't be included in the set:
set.add(1);set.add(5); // not addedset.add(7); // not addedconsole.log(set.size); // prints 3
You can check if a Set contains an element:
console.log(set.has(3)); // false
And convert a Set object into an array:
const array1 = [... set]; // spread operator
A Map can be more efficient than using an object ...
Read now
Unlock full access