Map Object in ES 6

ES 6 introduced a new feature that is included in JavaScript. In the previous versions of ECMAScript, if we want to perform mapping on values and keys, we mostly use the objects. The object allows us to map any values and keys.

In ES 6, it is an additional feature to work with a new set of types known as a map. It is used to store the key pair, in which we can use any kind of key or values.

The map object recalls the keys insertion order. The value and keys in the map can be treated as an object or primitive. The map object often used to return an empty or new map. The maps are defined as an ordered form so that they can traverse the elements.

Syntax

Here, we have the syntax to create a map.

Methods of Maps

The term map object contains various methods. They are given below in the tabular form-

             Sr. No       Method Name          Description
                 1Map.prototype.delete(key)This method is used for deleting the entries.
                 2Map.prototype.clear()This method is mostly used to delete value pairs and keys from the map object.
                 3Map.protoype.entries()This method is used to return the iterator object, which contains an array with key values pairs. The elements in Map are always in the insertion order.
                 4Map.prototype.has(value)It is used to check that the corresponding keys present in the Map object or not.
                 5Map.prototype.keys()This method is used to return an iterator for all keys involved in the Map object.
                 6Map.prototype.values()This method is used to return an iterator for all the values stored in the map.
                 7Map.prototype.forEach(callbackFn[ thisArgs])This method is used to execute the callback function at single time. The function callback executes for each value of a map object.

Map Properties

Map.prototype.size

This property is used to return the various values in the Map object.

Syntax

Example

Output

After the execution of the code, we got the following output:

Map Object in ES 6

Weak Maps

In ES 6, the weak maps are the same as regular maps. But, the key difference is that the keys are considered as the object in weak maps. It means the map key is treated as an object, and the value is arbitrary value. The weak maps are used to store the elements in the form of key-value pairs with the weak referenced keys.

The object in the weak map always has an object type key. We don’t have any method to find the list of keys, so the keys are not treated as enumerable. The objects iterate the elements in an insertion manner in a weak map. If the key objects are without the reference, they will move into the garbage collection. The weak map object includes get(key), set(value, key), has(key), and delete (key).

Here, we have an example to understand this concept.

Example

Output

After the execution of the code, we got the following output:

Map Object in ES 6

Iterator and Map

The iterator is also called an object. It is used to determine the manner and returns result during the termination. We can access only a single set of object at a time. The Set and Map are used to return the iterators as output. The iterator object is associated with the next method (). If we call the next method (), it returns an object with value and done properties.

Here, we have some examples to understand the iterator associated with the map object.

Example 1

Output

After the execution of the above code, we got the following output:

Map Object in ES 6

Example 2

Output

After the execution of the code, we got the following output:

Map Object in ES 6

Example 3

Output

After the execution of the code, we got the following output:

Map Object in ES 6

Weak Maps with for…of Loop

In the Weak Maps, for…of loops are used to perform the iterations on the values and keys of Map object.

We have an example to understand the weak map with a for…of loop.

Example

Output

After the execution of the above example, we got the following output:

Map Object in ES 6

Pin It on Pinterest

Share This