Reliable JavaScript Certification Online #

ECMAScript 2015 introduced new data structures Map and Set, that store data ordered by key. Elements in Map and Set objects are iterated in the order in which they were inserted.

Maps

Map is an object that stores collection of key/value pairs. As a key or a value can be used any value, both primitives and objects. Elements in the Map object are iterated in the order in which they were inserted.

To create a new Map object use the syntax:

new Map([iterable]);

The iterable is an iterable object having key/value pairs eg. an array.

Map properties and methods

size – returns the number of elements in the Map object.

set(key, value) – set the value for the key in the Map object.

get(key) – returns the value for a given key. If value is not found, returns undefined.

has(key) – checks whether is set a value for the key and returns boolean value.

delete(key) – removes element from the map associated with the key. If the element was removed it returns true and if element did not exist in the map it returns false.

clear() – removes all elements from the map.

keys() – returns an iterator object that contains all keys of elements in the map.

values() – returns an iterator object that contains all values of elements in the map.

entries() – returns an iterator object that contains an array of [key, value] elements in the map

foreach(callbackFunc) – iterates over the map and calls calbackFunc for each element in the map.

Examples

var map = new Map();
map.set('Mon', 'Monday');
map.set('Tue', 'Tuesday');
map.set('Wed', 'Wednesday');

console.log(map.size); // 3

for (var [key, value] of map) {
    console.log(key + ' => ' + value);
}
// output:
// Mon => Monday
// Tue => Tuesday
// Wed => Wednesday

var obj = { x: 1 };
var obj2 = { x: 2 };

map.set(obj, obj2);
map.set(0, obj2);

console.log(map.has(obj)); // true
console.log(map.get(obj)); // { x: 2 }
console.log(map.get(0)); // { x: 2 }
console.log(map.get(1)); // undefined

map.delete(obj);
console.log(map.has(obj)); // false

map.forEach(function(value, key) {
  console.log(key + ' => ' + value);
});
// Output:
// Mon => Monday
// Tue => Tuesday
// Wed => Wednesday
// 0 => [object Object]

map.clear();
console.log(map.size); // 0

 

Sets

Set is an object that stores collection of unique values. Elements in the Set object are iterated in the order in which they were inserted.

To create a new Set object use the syntax:

new Set([iterable]);

Set properties and methods

size – returns the number of elements in the Set object

add(value) – adds a new element to the Set object.

has(value) – checks whether an element with the given value exists in the Set object and returns boolean value.

delete(value) – removes the element with the given value. If the element was removed it returns true and if the element did not exist in the set object it returns false.

clear() – removes all elements from the Set object.

values() – returns an iterator object that contains all values of elements in the set.

entries() returns an iterator object that contains an array of [value, value] elements in the set.

foreach(callbackFunc) – iterates over the set and calls calbackFunc for each element in the set.

Examples

var set = new Set();
set.add('triangle');
set.add('triangle');
set.add('square');

console.log(set.size); // 2
set.forEach(function(value) {
  console.log(value);
});
// Output:
// triangle
// square

var array = [1, 2];
set.add(array);

console.log(set.has(array)); // true
set.delete(array); 
console.log(set.has(array)); // false

set.add(3);
set.add({a: 3});

var values = set.values();
for (var value of values) {
    console.log(value);
}
// Output:
// triangle
// square
// 3
// { a: 3 }