Description
Set
objects are collections of unique values. You can iterate its elements in insertion order. A value in a Set
may only occur once; it is unique in the Set
‘s collection.
Ex:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | const mySet = new Set(); mySet.add(1); mySet.add("some text"); mySet.add("foo"); mySet.has(1); // true mySet.delete("foo"); mySet.size; // 2 for (const item of mySet) { console.log(item); } // 1 // "some text" |
Main methods
new Set([iterable])
Creates the set, and if an iterable
object is provided (usually an array), copies values from it into the set.
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.
Ex:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | const mySet1 = new Set(); mySet1.add(1); // Set(1) { 1 } mySet1.add(5); // Set(2) { 1, 5 } mySet1.add(5); // Set(2) { 1, 5 } mySet1.add("some text"); // Set(3) { 1, 5, 'some text' } const o = { a: 1, b: 2 }; mySet1.add(o); mySet1.add({ a: 1, b: 2 }); // o is referencing a different object, so this is okay mySet1.has(1); // true mySet1.has(3); // false, since 3 has not been added to the set mySet1.has(5); // true mySet1.has(Math.sqrt(25)); // true mySet1.has("Some Text".toLowerCase()); // true mySet1.has(o); // true mySet1.size; // 5 mySet1.delete(5); // removes 5 from the set mySet1.has(5); // false, 5 has been removed mySet1.size; // 4, since we just removed one value mySet1.add(5); // Set(5) { 1, 'some text', {...}, {...}, 5 } - a previously deleted item will be added as a new item, it will not retain its original position before deletion console.log(mySet1); // Set(5) { 1, "some text", {…}, {…}, 5 } |
Set composition
Method | Return type | Mathematical |
---|---|---|
A.difference(B) | Set | A \ B |
A.intersection(B) | Set | A ∩ B |
A.symmetricDifference(B) | Set | (A∖B) ∪ (B∖A) |
A.union(B) | Set | A ∪ B |
A.isDisjointFrom(B) | Boolean | A ∩ B = ∅ |
A.isSubsetOf(B) | Boolean | A ⊆ B |
A.isSupersetOf(B) | Boolean | A ⊇ B |
Instance methods
Set.prototype.difference()
Takes a set and returns a new set containing elements in this set but not in the given set.
Set.prototype.entries()
Returns a new iterator object that contains an array of [value, value]
for each element in the Set
object, in insertion order.
Set.prototype.forEach()
Calls callbackFn
once for each value present in the Set
object, in insertion order. If a thisArg
parameter is provided, it will be used as the this
value for each invocation of callbackFn
.
Set.prototype.intersection()
Takes a set and returns a new set containing elements in both this set and the given set.
Set.prototype.isDisjointFrom()
Takes a set and returns a boolean indicating if this set has no elements in common with the given set.
Set.prototype.isSubsetOf()
Takes a set and returns a boolean indicating if all elements of this set are in the given set.
Set.prototype.isSupersetOf()
Takes a set and returns a boolean indicating if all elements of the given set are in this set.
Set.prototype.keys()
An alias for Set.prototype.values().
Set.prototype.symmetricDifference()
Takes a set and returns a new set containing elements which are in either this set or the given set, but not in both.
Set.prototype.union()
Takes a set and returns a new set containing elements which are in either or both of this set and the given set.
Set.prototype.values()
Returns a new iterator object that yields the values for each element in the Set
object in insertion order.
Ex:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | for (const item of mySet1) { console.log(item); } // 1, "some text", { "a": 1, "b": 2 }, { "a": 1, "b": 2 }, 5 for (const item of mySet1.keys()) { console.log(item); } // 1, "some text", { "a": 1, "b": 2 }, { "a": 1, "b": 2 }, 5 for (const item of mySet1.values()) { console.log(item); } // 1, "some text", { "a": 1, "b": 2 }, { "a": 1, "b": 2 }, 5 // key and value are the same here for (const [key, value] of mySet1.entries()) { console.log(key); } // 1, "some text", { "a": 1, "b": 2 }, { "a": 1, "b": 2 }, 5 // Convert Set object to an Array object, with Array.from const myArr = Array.from(mySet1); // [1, "some text", {"a": 1, "b": 2}, {"a": 1, "b": 2}, 5] // the following will also work if run in an HTML document mySet1.add(document.body); mySet1.has(document.querySelector("body")); // true // converting between Set and Array const mySet2 = new Set([1, 2, 3, 4]); console.log(mySet2.size); // 4 console.log([...mySet2]); // [1, 2, 3, 4] // intersect can be simulated via const intersection = new Set([...mySet1].filter((x) => mySet2.has(x))); // difference can be simulated via const difference = new Set([...mySet1].filter((x) => !mySet2.has(x))); // Iterate set entries with forEach() mySet2.forEach((value) => { console.log(value); }); // 1 // 2 // 3 // 4 |
Implementing basic set operations
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | function isSuperset(set, subset) { for (const elem of subset) { if (!set.has(elem)) { return false; } } return true; } function union(setA, setB) { const _union = new Set(setA); for (const elem of setB) { _union.add(elem); } return _union; } function intersection(setA, setB) { const _intersection = new Set(); for (const elem of setB) { if (setA.has(elem)) { _intersection.add(elem); } } return _intersection; } function symmetricDifference(setA, setB) { const _difference = new Set(setA); for (const elem of setB) { if (_difference.has(elem)) { _difference.delete(elem); } else { _difference.add(elem); } } return _difference; } function difference(setA, setB) { const _difference = new Set(setA); for (const elem of setB) { _difference.delete(elem); } return _difference; } // Examples const setA = new Set([1, 2, 3, 4]); const setB = new Set([2, 3]); const setC = new Set([3, 4, 5, 6]); isSuperset(setA, setB); // returns true union(setA, setC); // returns Set {1, 2, 3, 4, 5, 6} intersection(setA, setC); // returns Set {3, 4} symmetricDifference(setA, setC); // returns Set {1, 2, 5, 6} difference(setA, setC); // returns Set {1, 2} |