Maps & Sets Flashcards
How do we create a new map?
let mapVar= new Map();
How do we check if a Map has a key of x?
mapVar.has(x);
How do we set a key par for map?
mapVar.set(key1,value1);
How do we get the value in any given map?
mapVar.get(key1);
How do we get the length of a map?
mapVar.size;
How do we get all the keys in a map?
mapVar.entries();
How do we get all the values in a map?
mapVar.values();
How do we create a set?
let test = new Set(…arr);
What is a set used for?
A set is used to for example take an array an remove all duplicates from it.
What is a weakmap?
A weak map is a Map however with 2 major changes, it is not enumerable so we cannot get the size of it or iterate over it using forEach(), and also it holds references to objects so if they were to be set to null in a map there would still be a reference but if it were referenced in a map we would receive an error when trying to recall that value.
All references that are gone end up being garbage collected.