JS Set & Map Flashcards

1
Q

Set

A

The Set object lets you store unique values of any type, whether primitive values or object references.

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.

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

Set.prototype.size

A

Returns the number of values in the Set object.

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

Set.prototype.add(value)

A

Inserts a new element with a specified value in to a Set object, if there isn’t an element with the same value already in the Set.

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

Set.prototype.clear()

Map.prototype.clear()

A

Removes all elements from the Set || Map object.

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

Set.prototype.delete(value)

A

Removes the element associated to the value and returns a boolean asserting whether an element was successfully removed or not. Set.prototype.has(value) will return false afterwards.

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

Set.prototype.has(value)

A

Returns a boolean asserting whether an element is present with the given value in the Set object or not.

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

What are the main methods of a Set?

A

.add(value)
.delete(value)
.has(value)

.clear()

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

Set.prototype.forEach(callbackFn[, thisArg])

A

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.

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

Set.prototype.values()

A

Returns a new iterator object that yields the values for each element in the Set object in insertion order.

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

Map

A

The Map object holds key-value pairs and remembers the original insertion order of the keys. Any value (both objects and primitive values) may be used as either a key or a value.

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

Objects vs. Maps

A

A Map does not contain any keys by default

A Maps keys can be any value (functions, objects, etc)
while an object has to be String or Symbol

The keys in Map are ordered in a simple, straightforward way

The number of items in a Map is easily retrieved from its size property.

A Map is an iterable, so it can be directly iterated.

A Map is better if there are frequent removals or additions

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

Map.prototype.size

A

Returns the number of key/value pairs in the Map object.

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

Map.prototype.get(key)

A

Returns the value associated to the key, or undefined if there is none.

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

Map.prototype.has(key)

A

Returns a boolean asserting whether a value has been associated to the key in the Map object or not.

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

Map.prototype.set(key, value)

A

Sets the value for the key in the Map object. Returns the Map object.

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

Map.prototype.keys()

A

Returns a new Iterator object that contains the keys for each element in the Map object in insertion order.

17
Q

Map.prototype.values()

A

Returns a new Iterator object that contains the values for each element in the Map object in insertion order.

18
Q

Map.prototype.entries()

A

Returns a new Iterator object that contains an array of [key, value] for each element in the Map object in insertion order.

19
Q

Map.prototype.forEach(callbackFn[, thisArg])

A

Calls callbackFn once for each key-value pair present in the Map object, in insertion order. If a thisArg parameter is provided to forEach, it will be used as the this value for each callback.

20
Q

Iterate a Map with a for…of loop

A

for (const [key, value] of myMap) {
console.log(${key} = ${value});
}

21
Q

Iterate a map with .forEach()

A

myMap.forEach((value, key) => {
console.log(${key} = ${value});
});

22
Q

How would you merge Maps?

A

const merged = new Map([…first, …second]);

const merged = new Map([…first, …second, [1, ‘eins’]]);

23
Q

iterate of a set with for…of

A

for (const item of mySet1) {
console.log(item);
}

24
Q

iterate over a Set with .forEach()

A

mySet2.forEach((value) => {
console.log(value);
});

25
Q

How can you remove duplicate elements in an Array?

A

arraySet = …new Set(array);

26
Q

How can you remove duplicates from a string?

A

(case-sensitive)

new Set(“Firefox”) // Set(7) { “F”, “i”, “r”, “e”, “f”, “o”, “x” }

new Set(“firefox”) // Set(6) { “f”, “i”, “r”, “e”, “o”, “x” }

27
Q

Use Set to ensure the uniqueness of a list

A
const array = Array
  .from(document.querySelectorAll('[id]'))
  .map((e) => e.id);
const set = new Set(array);
console.assert(set.size === array.length);
28
Q

What are the main methods of Map?

A

.set(key, value)
.delete(key)
.has(key)
.get(key)

.clear()