Set API Flashcards

1
Q

What is a Set?

A

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).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Set() constructor

A

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](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for…of 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);
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Set[@@species] static accessor

A

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;
  }
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Set.prototype[@@iterator]()

A

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); // {}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Set.prototype.add()

A

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"]
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Set.prototype.clear()

A

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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Set.prototype.delete()

A

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);
  }
});
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Set.prototype.difference()

A

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)

Parameters
other - 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 }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Set.prototype.entries()

A

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"]
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Set.prototype.forEach()

A

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 as value.
  • set - The set being iterated.

thisArg Optional - A value to use as this when executing callbackFn.

Return value
None (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"
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Set.prototype.has()

A

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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Set.prototype.intersection()

A

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 }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Set.prototype.isDisjointFrom()

A

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 value
true 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Set.prototype.isSubsetOf()

A

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)

Parameters
other - A Set object, or set-like object.

Return value
true 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Set.prototype.isSupersetOf()

A

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)

Parameters
other - A Set object, or set-like object.

Return value
true 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Set.prototype.keys()

A

The keys() method of Set instances is an alias for the values() method.

Syntax

keys()

Parameters
None.

Return value
A new iterable iterator object.

Examples

const mySet = new Set();
mySet.add("foo");
mySet.add("bar");
mySet.add("baz");

const setIter = mySet.keys();

console.log(setIter.next().value); // "foo"
console.log(setIter.next().value); // "bar"
console.log(setIter.next().value); // "baz"
17
Q

Set.prototype[Symbol.iterator]()

A

The [Symbol.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

const mySet = new Set();
mySet.add("0");
mySet.add(1);
mySet.add({});

for (const v of mySet) {
  console.log(v);
}
18
Q

Set.prototype.symmetricDifference()

A

The symmetricDifference() method of Set instances takes a set and returns a new set containing elements which are in either this set or the given set, but not in both.

Syntax

symmetricDifference(other)

Parameters

other - A Set object, or set-like object.

Return value
A new Set object containing elements which are in either this set or the other set, but not in both.

19
Q

Set.prototype.union()

A

The union() method of Set instances takes a set and returns a new set containing elements which are in either or both of this set and the given set.

Syntax

union(other)

Parameters
other - A Set object, or set-like object.

Return value
A new Set object containing elements which are in either or both of this set and the other set.

20
Q

Set.prototype.values()

A

The values() method of Set instances returns a new set iterator this set in insertion order.

Syntax

values()

Parameters
None.

Return value
A new iterable iterator object.

Examples

const mySet = new Set();
mySet.add("foo");
mySet.add("bar");
mySet.add("baz");

const setIter = mySet.values();

console.log(setIter.next().value); // "foo"
console.log(setIter.next().value); // "bar"
console.log(setIter.next().value); // "baz"
21
Q

Set.prototype.size

A

The size accessor property of Set instances returns the number of (unique) elements in this set.

Description
The value of size is an integer representing how many entries the Set object has. A set accessor function for size is undefined; you cannot change this property.

Examples

const mySet = new Set();
mySet.add(1);
mySet.add(5);
mySet.add("some text");

console.log(mySet.size); // 3