Set (Hash Set)
- A
Set is a collection of values, where each value may occur only once.
Methods
new Set(iterable) – creates the set, optionally from an array of values (any iterable will do).
set.add(value) – adds a value, returns the set itself.
set.delete(value) – removes the value, returns true if value existed at the moment of the call, otherwise false.
set.has(value) – returns true if the value exists in the set, otherwise false.
set.clear() – removes everything from the set.
set.size – is the elements count.
let set = new Set();
let john = { name: "John" };
let pete = { name: "Pete" };
let mary = { name: "Mary" };
// visits, some users come multiple times
set.add(john);
set.add(pete);
set.add(mary);
set.add(john);
set.add(mary);
// set keeps only unique values
alert( set.size ); // 3
for (let user of set) {
alert(user.name); // John (then Pete and Mary)
}
- The alternative to Set could be an array of users, and the code to check for duplicates on every insertion using arr.find.
- The performance would be much worse, because this method walks through the whole array checking every element.
- Set is much better optimized internally for uniqueness checks.
- We can loop over a set either with for..of or using forEach:
let set = new Set(["oranges", "apples", "bananas"]);
for (let value of set) alert(value);
// the same with forEach:
set.forEach((value, valueAgain, set) => {
alert(value);
});
- The
forEach function in the Set has 3 arguments: a value, then again a value, and then the target object.
- Indeed, the same value appears in the arguments twice.
- That’s for compatibility with
Map where forEach has three arguments.
- It may help to replace
Map with Set in certain cases with ease, and vice versa.
- The same methods
Map has for iterators are also supported:
set.keys() – returns an iterable object for values,
set.values() – same as set.keys, for compatibility with Map,
set.entries() – returns an iterable object for entries [value, value], exists for compatibility with Map.
Map (Dictionary)