Map and Set Flashcards
_____are used for storing keyed collections.
______are used for storing ordered collections.
Objects
Arrays
Map is a collection of keyed data items, just like an Object. But the main difference is that Map allows_______
keys of any type.
unlike objects, keys are not converted to ______
unlike objects, keys are not converted to strings
creates the map
new Map()
The method sets and stores the value by the key.
map.set(key, value)
returns the value by the key, undefined if key doesn’t exist in map.
map.get(key) –
returns true if the key exists, false otherwise.
map.has(key)
removes the element (the key/value pair) by the key.
map.delete(key) –
removes everything from the map
map.clear()
returns the current element count.
map.size
Map can also use objects as _____
keys
let john = { name: “John” };
let visitsCountMap = new Map();
set map to return below
alert( visitsCountMap.get(john) ); // 123
visitsCountMap.set(john, 123);
For looping over a map, there are 3 methods:
map.keys() – returns an iterable for keys,
map.values() – returns an iterable for values, map.entries() – returns an iterable for entries [key, value], it’s used by default in for..of.
let recipeMap = new Map([
[‘cucumber’, 500],
[‘tomatoes’, 350],
[‘onion’, 50]
]);
// iterate over keys (vegetables)
for (let vegetable of recipeMap.keys()) {
alert(vegetable); // cucumber, tomatoes, onion
}
let recipeMap = new Map([
[‘cucumber’, 500],
[‘tomatoes’, 350],
[‘onion’, 50]
]);
// iterate over values (amounts)
for (let amount of recipeMap.values()) {
alert(amount); // 500, 350, 50
}
let recipeMap = new Map([
[‘cucumber’, 500],
[‘tomatoes’, 350],
[‘onion’, 50]
]);
// iterate over [key, value] entries
for (let entry of recipeMap) { // the same as of recipeMap.entries()
alert(entry); // cucumber,500 (and so on)
}
The iteration goes in the same order as the values were inserted. Map preserves this order, unlike a regular Object.
true or false
true
let map = new Map([
[‘1’, ‘str1’],
[1, ‘num1’],
[true, ‘bool1’]
]);
alert( map.get(‘1’) ); //
str1
let map = new Map([
[‘1’, ‘str1’],
[1, ‘num1’],
[true, ‘bool1’]
]);
alert( ________ ); // true
what code to write?
map.get(true)
let obj = {
name: “John”,
age: 30
};
let map = new Map(Object.entries(obj));
alert( map.get(‘name’) ); //
John
let obj = {
name: “John”,
age: 30
};
let map =
alert( map.get(‘name’) ); // John
new Map(Object.entries(obj));
_____________ method that does the reverse: given an array of [key, value] pairs, it creates an object from them:
Object.fromEntries
let prices = Object.fromEntries([
[‘banana’, 1],
[‘orange’, 2],
[‘meat’, 4]
]);
// now prices =
{ banana: 1, orange: 2, meat: 4 }
let prices = Object.fromEntries([
[‘banana’, 1],
[‘orange’, 2],
[‘meat’, 4]
]);
// now prices = { banana: 1, orange: 2, meat: 4 }
alert(prices.orange); //
2
let set = // create new empty set
new Set()
let set = new Set();
let john = { name: “John” };
let pete = { name: “Pete” };
let mary = { name: “Mary” };
// visits, some users come multiple times
set.add(john);
set.add(pete);
set.add(mary);
set.add(john);
set.add(mary);
// set keeps only unique values
alert( set.size ); // returns and why
3
keeps only unique values
______– adds a value, returns the set itself.
set.add(value)
__________– removes the value, returns true if value existed at the moment of the call, otherwise false.
set.delete(value)
___________– returns true if the value exists in the set, otherwise false.
set.has(value)
______________ removes everything from the set.
set.clear() –
__________ – is the elements count.
set.size
We can loop over a set either with
for..of
forEach:
let set = new Set();
let john = { name: “John” };
let pete = { name: “Pete” };
let mary = { name: “Mary” };
set.add(john);
set.add(pete);
set.add(mary);
for (let user of set) {
alert(user.name); //
}
John (then Pete and Mary)
let set = new Set();
let john = { name: “John” };
let pete = { name: “Pete” };
let mary = { name: “Mary” };
set.add(john);
set.add(pete);
set.add(mary);