Keyed Collections Flashcards
1
Q
What is the difference between a Map and an Object in JS?
A
- A Map does not contain any keys by default whereas an object has a prototype so it contains default keys e.g. “toString” is defined.
- A Map’s keys can be ANY value (even a function) whereas an Object’s keys can only be Strings or Symbols.
- Map has a size PROPERTY (i.e. map.size) to calculate number of items in a map, Object does not.
- You can iterate over a map using for…of, with an Object you can only use for….in or retrieve the entries or keys of the object.
- Map performs better in scenarios involving frequent additions and removals of key-value pairs.
2
Q
How do you put data into a map?
A
map.set(key,value)
3
Q
How do you remove all key value pairs from a map?
A
map.clear()
4
Q
How do you remove one key value pair from a map?
A
map.delete(key)