Skip to content

Latest commit

 

History

History
42 lines (31 loc) · 904 Bytes

mapObject.md

File metadata and controls

42 lines (31 loc) · 904 Bytes

The Map() object

The Map object has key-value pairs and remembers the original insertion order of the keys.

It differs from the normal Object because:

  • Any values can be used as a key or value. (In Object, keys have to be Strings or Symbols).
  • The keys in Map are ordered, so iterating over the object will return the keys in order of insertion.
  • Using the size property on a Map will return the size of a Map.
  • Map objects are iterable.

Example:

const cities = new Map([
  [0, 'Paris'],
  [1, 'London'],
  [2, 'Sydney']
])

for (var value of cities.value()){
  console.log(value)
}

or

var cities = new Map()

cities.set('0', "Paris");
cities.set({}, "London");
cities.set(() => {}, "Sydney");

for (var [key, value] of cities.entries()) {
  console.log(key + ' = ' + value);
}

// 0 = Paris
// [object Object] = London
// () => {} = Sydney