ES6 Map & Set part 1

JavaScript Tutorial
ECMAScript 6
ES6 Map & Set
19
1/2
         

ES6 Map



Map object can be used to hold key/valuepairs. A key or value in a map can be anything (objects and primitive values). 

The syntax new Map([iterable]) creates a Map object where iterable is an array or any other iterable object whose elements are arrays (with a key/value pair each).

An Object is similar to Map but there are important differences that make using a Map preferable in certain cases:
1) The keys can by any type including functions, objects, and any primitive.
2) You can get the size of a Map.
3) You can directly iterate over Map.
4) Performance of the Map is better in scenarios involving frequent addition and removal of key/value pairs.

The size property returns the number of key/value pairs in a map.
For example:
let map = new Map([['k1', 'v1'], ['k2', 'v2']]);

console.log(map.size); // 2
Try It Yourself

Methods
set(key, value) Adds a specified key/value pair to the map. If the specified key already exists, value corresponding to it is replaced with the specified value.
get(key) Gets the value corresponding to a specified key in the map. If the specified key doesn't exist, undefined is returned.
has(key) Returns true if a specified key exists in the map and false otherwise.
delete(key) Deletes the key/value pair with a specified key from the map and returns true. Returns false if the element does not exist.
clear() Removes all key/value pairs from map.
keys() Returns an Iterator of keys in the map for each element.
values() Returns an Iterator of values in the map for each element.
entries() Returns an Iterator of array[key, value] in the map for each element.

For example:
let map = new Map();

map.set('k1', 'v1').set('k2', 'v2');

console.log(map.get('k1')); // v1

console.log(map.has('k2')); // true

for (let kv of map.entries())
console.log(kv[0] + " : " + kv[1]);
Try It Yourself

The above example demonstrates some of the ES6 Map methods.
Map supports different data types i.e. 1 and "1" are two different keys/values.

Comments