JS Set & Map Flashcards
Set
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.
Set.prototype.size
Returns the number of values in the Set object.
Set.prototype.add(value)
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.
Set.prototype.clear()
Map.prototype.clear()
Removes all elements from the Set || Map object.
Set.prototype.delete(value)
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.
Set.prototype.has(value)
Returns a boolean asserting whether an element is present with the given value in the Set object or not.
What are the main methods of a Set?
.add(value)
.delete(value)
.has(value)
.clear()
Set.prototype.forEach(callbackFn[, thisArg])
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.values()
Returns a new iterator object that yields the values for each element in the Set object in insertion order.
Map
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.
Objects vs. Maps
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
Map.prototype.size
Returns the number of key/value pairs in the Map object.
Map.prototype.get(key)
Returns the value associated to the key, or undefined if there is none.
Map.prototype.has(key)
Returns a boolean asserting whether a value has been associated to the key in the Map object or not.
Map.prototype.set(key, value)
Sets the value for the key in the Map object. Returns the Map object.
Map.prototype.keys()
Returns a new Iterator object that contains the keys for each element in the Map object in insertion order.
Map.prototype.values()
Returns a new Iterator object that contains the values for each element in the Map object in insertion order.
Map.prototype.entries()
Returns a new Iterator object that contains an array of [key, value] for each element in the Map object in insertion order.
Map.prototype.forEach(callbackFn[, thisArg])
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.
Iterate a Map with a for…of loop
for (const [key, value] of myMap) {
console.log(${key} = ${value}
);
}
Iterate a map with .forEach()
myMap.forEach((value, key) => {
console.log(${key} = ${value}
);
});
How would you merge Maps?
const merged = new Map([…first, …second]);
const merged = new Map([…first, …second, [1, ‘eins’]]);
iterate of a set with for…of
for (const item of mySet1) {
console.log(item);
}
iterate over a Set with .forEach()
mySet2.forEach((value) => {
console.log(value);
});
How can you remove duplicate elements in an Array?
arraySet = …new Set(array);
How can you remove duplicates from a string?
(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” }
Use Set to ensure the uniqueness of a list
const array = Array .from(document.querySelectorAll('[id]')) .map((e) => e.id);
const set = new Set(array); console.assert(set.size === array.length);
What are the main methods of Map?
.set(key, value)
.delete(key)
.has(key)
.get(key)
.clear()