Map
is a key/value pair, way more flexible than object
Map()
is a global constructor function
new Map()
creates a new empty map
the API basics
WHEN passing an array of key+value pairs to new Map()
THEN the map is initialized with those pairs
GIVEN an empty Map WHEN calling map.get(42)
THEN it returns undefined
GIVEN a Map that has the key 42
WHEN calling map.get(42)
THEN it returns the value of the pair
GIVEN an empty Map WHEN calling map.set(42, "forty-two")
THEN the value is added to the map
WHEN calling map.has("key")
THEN it tells if map has "key"
WHEN reading the size
property of a Map THEN it tells how many items are in the map
WHEN calling delete("key")
THEN the element with the key "key" is removed AND map.size
reports the number of items left
in depth features
map.keys()
and map.values()
return iterables
a Map
can be iterated over using for-of
complex types can be keys
Links
MDN page about Map.
The chapter in the specification, with all details about Map.
Required Knowledge
- `const` declaration (Block scope)
Related Katas
Map
- basics
map.get()
map.set()
- initialize
map.has()
Difficulty Level
BEGINNER
First Published
21 May 2015
Stats
12 tests to solve