Set API Flashcards
What is a Set
?
Set objects are collections of values. A value in the set may only occur once; it is unique in the set’s collection. You can iterate through the elements of a set in insertion order. The insertion order corresponds to the order in which each element was inserted into the set by the add()
method successfully (that is, there wasn’t an identical element already in the set when add()
was called).
“Set
- JavaScript | MDN” (MDN Web Docs). Retrieved July 2, 2024.
Set()
constructor
The Set()
constructor creates Set objects.
Syntax
new Set() new Set(iterable)
Note: Set()
can only be constructed with new
. Attempting to call it without new throws a TypeError
.
Parameters
iterable
Optional - If an iterable object is passed, all of its elements will be added to the new Set
.
If you don’t specify this parameter, or its value is null
, the new Set
is empty.
Return value
A new Set
object.
Examples
const mySet = new Set(); mySet.add(1); // Set [ 1 ] mySet.add(5); // Set [ 1, 5 ] mySet.add(5); // Set [ 1, 5 ] mySet.add("some text"); // Set [ 1, 5, 'some text' ] const o = { a: 1, b: 2 }; mySet.add(o);
“Set()
constructor - JavaScript | MDN” (MDN Web Docs). Retrieved July 2, 2024.
Set[@@species]
static accessor
The Set[@@species]
static accessor property is an unused accessor property specifying how to copy Set
objects.
Syntax
Set[Symbol.species]
Return value
The value of the constructor (this
) on which get @@species
was called. The return value is used to construct copied Set
instances.
Description
The @@species
accessor property returns the default constructor for Set
objects. Subclass constructors may override it to change the constructor assignment.
Note: This property is currently unused by all Set
methods.
Examples
Species in ordinary objects
The @@species
property returns the default constructor function, which is the Set
constructor for Set
.
Set[Symbol.species]; // function Set()
Species in derived objects
In an instance of a custom Set
subclass, such as MySet
, the MySet
species is the MySet
constructor. However, you might want to overwrite this, in order to return parent Set
objects in your derived class methods:
class MySet extends Set { // Overwrite MySet species to the parent Set constructor static get [Symbol.species]() { return Set; } }
“Set[@@species]
- JavaScript | MDN” (MDN Web Docs). Retrieved July 2, 2024.
Set.prototype[@@iterator]()
The [@@iterator]()
method of Set
instances implements the iterable protocol and allows Set
objects to be consumed by most syntaxes expecting iterables, such as the spread syntax and for...of
loops. It returns a set iterator object that yields the values of the set in insertion order.
The initial value of this property is the same function object as the initial value of the Set.prototype.values
property.
Syntax
set[Symbol.iterator]()
Parameters
None.
Return value
The same return value as Set.prototype.values()
: a new iterable iterator object that yields the values of the set.
Examples:
Iteration using for...of
loop.
const mySet = new Set(); mySet.add("0"); mySet.add(1); mySet.add({}); for (const v of mySet) { console.log(v); }
Manually hand-rolling the iterator
const mySet = new Set(); mySet.add("0"); mySet.add(1); mySet.add({}); const setIter = mySet[Symbol.iterator](); console.log(setIter.next().value); // "0" console.log(setIter.next().value); // 1 console.log(setIter.next().value); // {}
“Set.prototype[@@iterator]()
- JavaScript | MDN” (MDN Web Docs). Retrieved July 3, 2024.
Set.prototype.add()
The add()
method of Set
instances inserts a new element with a specified value in to this set, if there isn’t an element with the same value already in this set.
Syntax
add(value)
Parameters
value
- The value of the element to add to the Set
object.
Return value
The Set
object with added value.
Examples
const mySet = new Set(); mySet.add(1); mySet.add(5).add("some text"); // chainable console.log(mySet); // Set [1, 5, "some text"]
“Set.prototype.add()
- JavaScript | MDN” (MDN Web Docs). Retrieved July 3, 2024.
Set.prototype.clear()
The clear()
method of Set
instances removes all elements from this set.
Syntax
clear()
Parameters
None.
Return value
None (undefined
).
Examples
const mySet = new Set(); mySet.add(1); mySet.add("foo"); console.log(mySet.size); // 2 console.log(mySet.has("foo")); // true mySet.clear(); console.log(mySet.size); // 0 console.log(mySet.has("foo")); // false
“Set.prototype.clear()
- JavaScript | MDN” (MDN Web Docs). Retrieved July 3, 2024.
Set.prototype.delete()
The delete()
method of Set
instances removes a specified value from this set, if it is in the set.
Syntax
setInstance.delete(value)
Parameters
value
- The value to remove from Set
.
Return value
Returns true
if value was already in Set
; otherwise false
.
Examples
const mySet = new Set(); mySet.add("foo"); console.log(mySet.delete("bar")); // false; no "bar" element found to be deleted. console.log(mySet.delete("foo")); // true; successfully removed. console.log(mySet.has("foo")); // false; the "foo" element is no longer present.
Deleting an object from a set
Because objects are compared by reference, you have to delete them by checking individual properties if you don’t have a reference to the original object.
const setObj = new Set(); // Create a new set. setObj.add({ x: 10, y: 20 }); // Add object in the set. setObj.add({ x: 20, y: 30 }); // Add object in the set. // Delete any point with `x > 10`. setObj.forEach((point) => { if (point.x > 10) { setObj.delete(point); } });
“Set.prototype.delete()
- JavaScript | MDN” (MDN Web Docs). Retrieved July 5, 2024.
Set.prototype.difference()
The difference()
method of Set
instances takes a set and returns a new set containing elements in this set but not in the given set.
Syntax
difference(other)
Parametersother
- A Set
object, or set-like object.
Return value
A new Set
object containing elements in this set but not in the other
set.
Examples
const odds = new Set([1, 3, 5, 7, 9]); const squares = new Set([1, 4, 9]); console.log(odds.difference(squares)); // Set(3) { 3, 5, 7 }
“Set.prototype.difference()
- JavaScript | MDN” (MDN Web Docs). Retrieved July 16, 2024.
Set.prototype.entries()
The entries()
method of Set
instances returns a new set iterator object that contains an array of [value, value]
for each element in this set, in insertion order. For Set
objects there is no key
like in Map
objects. However, to keep the API similar to the Map
object, each entry has the same value for its key
and value
here, so that an array [value, value]
is returned.
Syntax
entries()
Parameters
None.
Return value
A new iterable iterator object.
Examples
const mySet = new Set(); mySet.add("foobar"); mySet.add(1); mySet.add("baz"); const setIter = mySet.entries(); console.log(setIter.next().value); // ["foobar", "foobar"] console.log(setIter.next().value); // [1, 1] console.log(setIter.next().value); // ["baz", "baz"]
“Set.prototype.entries() - JavaScript | MDN” (MDN Web Docs). Retrieved July 16, 2024.
Set.prototype.forEach()
The forEach()
method of Set
instances executes a provided function once for each value in this set, in insertion order.
Syntax
forEach(callbackFn) forEach(callbackFn, thisArg)
Parameters
callbackFn
- A function to execute for each entry in the set. The function is called with the following arguments:
-
value
- Value of each iteration. -
key
- Key of each iteration. This is always the same asvalue
. -
set
- The set being iterated.
thisArg
Optional - A value to use as this when executing callbackFn
.
Return valueNone
(undefined
).
Examples
function logSetElements(value1, value2, set) { console.log(`s[${value1}] = ${value2}`); } new Set(["foo", "bar", undefined]).forEach(logSetElements); // Logs: // "s[foo] = foo" // "s[bar] = bar" // "s[undefined] = undefined"
“Set.prototype.forEach()
- JavaScript | MDN” (MDN Web Docs). Retrieved July 17, 2024.
Set.prototype.has()
The has()
method of Set
instances returns a boolean indicating whether an element with the specified value exists in this set or not.
Syntax
has(value)
Parameters
value
- The value to test for presence in the Set
object.
Return value
Returns true
if an element with the specified value exists in the Set
object; otherwise false
.
Examples
const mySet = new Set(); mySet.add("foo"); console.log(mySet.has("foo")); // true console.log(mySet.has("bar")); // false const set1 = new Set(); const obj1 = { key1: 1 }; set1.add(obj1); console.log(set1.has(obj1)); // true console.log(set1.has({ key1: 1 })); // false, because they are different object references console.log(set1.add({ key1: 1 })); // now set1 contains 2 entries
“Set.prototype.has()
- JavaScript | MDN” (MDN Web Docs). Retrieved July 17, 2024.
Set.prototype.intersection()
The intersection()
method of Set
instances takes a set and returns a new set containing elements in both this set and the given set.
Syntax
intersection(other)
Parameters
other
- A Set
object, or set-like object.
Return value
A new Set
object containing elements in both this set and the other set.
Examples
const odds = new Set([1, 3, 5, 7, 9]); const squares = new Set([1, 4, 9]); console.log(odds.intersection(squares)); // Set(2) { 1, 9 }
“Set.prototype.intersection()
- JavaScript | MDN” (MDN Web Docs). Retrieved July 18, 2024.
Set.prototype.isDisjointFrom()
The isDisjointFrom()
method of Set
instances takes a set and returns a boolean indicating if this set has no elements in common with the given set.
Syntax
isDisjointFrom(other)
Parameters
other
- A Set
object, or set-like object.
Return valuetrue
if this set has no elements in common with the other
set, and false
otherwise.
Examples:
const primes = new Set([2, 3, 5, 7, 11, 13, 17, 19]); const squares = new Set([1, 4, 9, 16]); console.log(primes.isDisjointFrom(squares)); // true const composites = new Set([4, 6, 8, 9, 10, 12, 14, 15, 16, 18]); const squares = new Set([1, 4, 9, 16]); console.log(composites.isDisjointFrom(squares)); // false
“Set.prototype.isDisjointFrom()
- JavaScript | MDN” (MDN Web Docs). Retrieved July 26, 2024.
Set.prototype.isSubsetOf()
The isSubsetOf()
method of Set
instances takes a set and returns a boolean indicating if all elements of this set are in the given set.
Syntax
isSubsetOf(other)
Parametersother
- A Set
object, or set-like object.
Return valuetrue
if all elements in this set are also in the other
set, and false
otherwise.
Examples
const fours = new Set([4, 8, 12, 16]); const evens = new Set([2, 4, 6, 8, 10, 12, 14, 16, 18]); console.log(fours.isSubsetOf(evens)); // true const primes = new Set([2, 3, 5, 7, 11, 13, 17, 19]); const odds = new Set([3, 5, 7, 9, 11, 13, 15, 17, 19]); console.log(primes.isSubsetOf(odds)); // false // Equivalent sets are subsets of each other: const set1 = new Set([1, 2, 3]); const set2 = new Set([1, 2, 3]); console.log(set1.isSubsetOf(set2)); // true console.log(set2.isSubsetOf(set1)); // true
“Set.prototype.isSubsetOf()
- JavaScript | MDN” (MDN Web Docs). Retrieved July 26, 2024.
Set.prototype.isSupersetOf()
The isSupersetOf()
method of Set
instances takes a set and returns a boolean indicating if all elements of the given set are in this set.
Syntax
isSupersetOf(other)
Parametersother
- A Set
object, or set-like object.
Return valuetrue
if all elements in the other
set are also in this set, and false
otherwise.
Examples
const evens = new Set([2, 4, 6, 8, 10, 12, 14, 16, 18]); const fours = new Set([4, 8, 12, 16]); console.log(evens.isSupersetOf(fours)); // true const primes = new Set([2, 3, 5, 7, 11, 13, 17, 19]); const odds = new Set([3, 5, 7, 9, 11, 13, 15, 17, 19]); console.log(odds.isSupersetOf(primes)); // false // Equivalent sets are supersets of each other: const set1 = new Set([1, 2, 3]); const set2 = new Set([1, 2, 3]); console.log(set1.isSupersetOf(set2)); // true console.log(set2.isSupersetOf(set1)); // true
“Set.prototype.isSupersetOf()
- JavaScript | MDN” (MDN Web Docs). Retrieved July 26, 2024.