Maps Flashcards
An Object has a prototype, so there are default keys in the map. However, this can be bypassed using map = __________.
Object.create(null)
The keys of an objects are __________, where they can be any value for a Map.
strings
Maps can be iterated using a
for..of loop:
ITERATE THROUGH THIS MAP for of
var myMap = new Map();
myMap.set(0, ‘zero’);
myMap.set(1, ‘one’);
// CODE HERE
// 0 = zero // 1 = one
for (var [key, value] of myMap) {
console.log(key + ‘ = ‘ + value);
}
ITERATE THROUGH THIS MAP for of
var myMap = new Map();
myMap.set(0, ‘zero’);
myMap.set(1, ‘one’);
// CODE HERE
// 0 // 1
for (var key of myMap.keys()) {
console.log(key);
}
ITERATE THROUGH THIS MAP for of
var myMap = new Map();
myMap.set(0, ‘zero’);
myMap.set(1, ‘one’);
// CODE HERE
// zero // one
for (var value of myMap.values()) {
console.log(value);
}
ITERATE THROUGH THIS MAP forEach
var myMap = new Map();
myMap.set(0, ‘zero’);
myMap.set(1, ‘one’);
// CODE HERE
// Will show 2 logs; first with “0 = zero” and second with “1 = one”
myMap.forEach(function(value, key) {
console.log(key + ‘ = ‘ + value);
});
var original = new Map([
[1, ‘one’]
]);
var clone = new Map(original);
console. log(clone.get(1)); //
console. log(original === clone); //
one
false. Useful for shallow comparison
let ages = new Map();
SET key to “Boris” and “Value” to 39
ages.set(“Boris”, 39);
____________are special objects per se, they are iterables with key value pair constructor that looks like a 2D array but acts like an object. They offer a better flexibility in terms of choosing our key values. A ______ can have a key value which can be a string, number, object or even NaN.
Maps
Maps
The _________method removes all elements from a Set object.
clear()
in a ____ you can not have 2 of the same items
set
convert a set to array const myArray =
array.from(name of set)
var myMap = new Map();
myMap.set(‘0’, ‘foo’);
myMap.set(1, ‘bar’);
myMap.set({}, ‘baz’);
var mapIter = myMap.keys();
console. log(________________); // “0”
console. log(_________________); // 1
console. log(_________________; // Object
mapIter.next().value
mapIter.next().value
mapIter.next().value
The _______ method executes a provided function once per each key/value pair in the Map object, in insertion order.
forEach()