Map

Overview

The Map object holds key-value pairs and remembers the original insertion order of the keys. Any value (both objects and primitive values) may be used as either a key or a value.

For more information see: Map (MDN).

Properties Summarized

TypeNameSummary

The size accessor property returns the number of elements in a Map object.

Methods Summarized

TypeNameSummary

void

The clear() method removes all elements from a Map object.

The delete() method removes the specified element from a Map object by key.

The entries() method returns a new iterator object that contains the [key, value] pairs for each element in the Map object in insertion order.

void

The forEach() method executes a provided function once for each value in the Map object, in insertion order.

The get() method returns a specified element from a Map object.

The has() method returns a boolean indicating whether an element with the specified key exists in a Map object or not.

The keys() method returns a new iterator object that contains the keys for each element in the Map object in insertion order.

The set() method adds or updates an entry in a Map object with a specified key and a value.

The values() method returns a new iterator object that contains the values for each element in the Map object in insertion order.

Properties Detailed

size

The size accessor property returns the number of elements in a Map object.

Type Number

Sample

map.size;

Methods Detailed

clear()

The clear() method removes all elements from a Map object.

Returns: void

Sample

map.clear();

delete(key)

The delete() method removes the specified element from a Map object by key.

Parameters

Returns: Boolean

Sample

var success = map.delete(key);

entries()

The entries() method returns a new iterator object that contains the [key, value] pairs for each element in the Map object in insertion order. In this particular case, this iterator object is also an iterable, so the for-of loop can be used..

Returns: Iterator the iterator that can be used in for of loops

Sample

for(var entry of map.entries()) {}

forEach(callback, thisArgument)

The forEach() method executes a provided function once for each value in the Map object, in insertion order.

Parameters

Returns: void

Sample

map.forEach(function(keyValuePair) {});

get(key)

The get() method returns a specified element from a Map object. If the value that is associated to the provided key is an object, then you will get a reference to that object and any change made to that object will effectively modify it inside the Map object.

Parameters

Returns: Object

Sample

var value = map.get(key);

has(key)

The has() method returns a boolean indicating whether an element with the specified key exists in a Map object or not.

Parameters

Returns: Boolean

Sample

var containsKey = map.has(key);

keys()

The keys() method returns a new iterator object that contains the keys for each element in the Map object in insertion order. In this particular case, this iterator object is also an iterable, so a for...of loop can be used.

Returns: Iterator the iterator that can be used in for of loops

Sample

var values = map.keys();

set(key, value)

The set() method adds or updates an entry in a Map object with a specified key and a value.

Parameters

Returns: Map

Sample

map.set(keyObject, value);

values()

The values() method returns a new iterator object that contains the values for each element in the Map object in insertion order.

Returns: Iterator the iterator that can be used in for of loops

Sample

var values = map.values();

Last updated