Map and Set Flashcards
What is a map object?
An object with keyed data items but can take keys of any type. Strings Numbers Boolean Objects Null Undefined Infinity NaN
Create a new map
new Map()
Store a value by the key in a map object
Map.prototype.set(key, value)
Return the value by the key in a map object, undefined if key doesn’t exist in map.
Map.prototype.get(key)
Return true if the key exists in a map object, false otherwise.
Map.prototype.has(key)
Removes the value by the key in a map object
Map.prototype.delete(key)
Remove everything from the map object and return undefined
Map.prototype.clear()
Returns the current element count in a map object
Map.prototype.size
What does: new Map() do and/or return?
Create a new map
What does:
Map.prototype.set()
do and/or return?
Store a value by the key in a map object
Map.prototype.set(key, value)
Returns the map itself (can chain)
What does:
Map.prototype.get(key)
do and/or return?
Return the value by the key in a map object, undefined if key doesn’t exist in map.
What does:
Map.prototype.has(key)
do and/or return?
Return true if the key exists in a map object, false otherwise.
What does:
Map.prototype.delete(key)
do and/or return?
Removes the value by the key in a map object
Returns true or false based on whether the key existed or not.
What does:
Map.prototype.clear()
do and/or return?
Remove everything from the map object
Returns undefined
What does:
Map.prototype.size
do and/or return?
Returns the current element count in a map object
Does:
map[key]
map.key
work with map objects?
Yes, and they will behave like regular objects in this way
Returns iterables of keys, entries or values of a map objects
Map.prototype.keys()
Map.prototype.values()
Map.prototype.entries() (it’s used by default in for..of.
)
What order do map objects iterate over keys, values and entries?
The order they were added.
What does:
Map.prototype.forEach()
do and/or return?
.forEach does a function for each value in the map object
returns undefined
Do a function for each item in the map object
2 ways
Map.prototype.forEach()
or use: Map.prototype.keys() Map.prototype.values() Map.prototype.entries() or the map itself (which is the same as .entries()) and a for of loop
Create a new map objects with an array passed as an arugment to initiate some key/value pairs
let map = new Map([ ['1', 'str1'], [1, 'num1'], [true, 'bool1'] ]);
Create a map object from a regular objects
Object.entries(obj) returns an array of the objects entries
Map objects can take an array at initialization to create some initial key/value pairs.
let obj = { name: "John", age: 30 };
let map = new Map(Object.entries(obj));
alert( map.get(‘name’) ); // John
Create a regular object from a map (2 ways, similar. 1 better because shorter)
Object.fromEntries() can take an iterable object and create an object.
map.entries() can create an iterable object from the map object’s entries.(though map objects are already iterable)
So: let someMaps= new Map(); someMaps.set('banana', 1); someMaps.set('orange', 2); someMaps.set('meat', 4);
let obj = Object.fromEntries(someMaps.entries());
But: let obj = Object.fromEntries(map); // omit .entries() Because Object.fromEntries expects an iterable object as the argument. Not necessarily an array. And the standard iteration for map returns same key/value pairs as map.entries(). So we get a plain object with same key/values as the map.
What is a set
a special type of collection without keys. just sets of values. E