Keyed Collections Flashcards

1
Q

What is the difference between a Map and an Object in JS?

A
  1. 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.
  2. A Map’s keys can be ANY value (even a function) whereas an Object’s keys can only be Strings or Symbols.
  3. Map has a size PROPERTY (i.e. map.size) to calculate number of items in a map, Object does not.
  4. 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.
  5. Map performs better in scenarios involving frequent additions and removals of key-value pairs.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How do you put data into a map?

A

map.set(key,value)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How do you remove all key value pairs from a map?

A

map.clear()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How do you remove one key value pair from a map?

A

map.delete(key)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly