Map Object in ES 6

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.

var map = new map([iterables]);                  // map() accepts iterable objects

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

Map.size

Example

var map = new Map ();
map.set (‘David’, ‘Writer’);
map.set (‘Harris’, ‘Director’);
map.set (‘Bob’, ‘Actor’);
map.set (‘Willson’, ‘Designer’);
console.log (map.size);

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

‘use strict’
let abc = new Weakmap();
let object = {};
console.log (abc.set(objact, “Hello ES6”));
console.log (abc.has(object));

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

‘use strict’
var rainbow = new Map ([
[‘1’, ‘Blue’],
[‘2’, ‘Orange’],
[‘3’, ‘Purple’],
[‘4’, ‘Black’]
]);
var iterator = rainbow.values();
console.log (iterator.next());
console.log (iterator.next());
console.log (iterator.next());

Output

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

Map Object in ES 6

Example 2

‘use strict’
var rainbow = new Map ([
[‘1’, ‘Blue’],
[‘2’, ‘Orange’],
[‘3’, ‘Purple’],
[‘4’, ‘Black’]
]);
var iterator = rainbow.entries();
console.log (iterator.next());
console.log (iterator.next());
console.log (iterator.next());

Output

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

Map Object in ES 6

Example 3

‘use strict’
var rainbow = new Map ([
[‘1’, ‘Blue’],
[‘2’, ‘Orange’],
[‘3’, ‘Purple’],
[‘4’, ‘Black’]
]);
var iterator = rainbow.keys();
console.log (iterator.next());
console.log (iterator.next());
console.log (iterator.next());

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

‘use strict’
var rainbow = new Map ([
[‘1’, ‘Blue’],
[‘2’, ‘Orange’],
[‘3’, ‘Purple’],
[‘4’, ‘Black’]
]);
for (let rain of rainbow.values())
{
          console.log (rain);
}
console.log (“ ”)
for (let rain of rainbow.entries())
console.log (‘${rain[0]}: ${rain[1]}’);

Output

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

Map Object in ES 6

Related Topics

Promises in ES6

Promises in ES6 A promise is used to represent some aspects that are finally fulfilled. It can also be resolved or discarded according to the operational output. In ES6, a promise...

8 minutes read.

Events in ES6

Events in ES6 The events can be defined as the actions or the occurrence of an action that is perceived by the software. The event is used to handle the interaction between...

14 minutes read.

Class in ES6

Class in ES6: The Class is a basic term in object-oriented programming (OOPs). A class helps to determine the prototype for modeling in real-world objects. It is used to formulate...

4 minutes read.

Array in ES6

Array in ES6 The array can be defined as an object that is used to display the set of homogenous type of elements in ECMAScript 6. The array provides the facility to store...

5 minutes read.

Spread Operator in ES6

Spread Operator in ECMAScript 6 The term 'Spread Operator' can be defined as an operator that allows expending the array into separate elements. Three dots (…) isthe syntax of the Spread Operator.The Spread operator...

5 minutes read.

Module in ES6

Module in ES6 In ES6, the modules can be defined as a small chunk of program code stored in a file. The modules are used to modularize the source code with...

6 minutes read.

Cookies in ES6

Cookies in ES6 The cookies are an old client-side storage technique produced in a server-side scripting language such as Php and ASP etc. The cookie can be defined as a small...

6 minutes read.

Boolean in ES6

Boolean in ES6 In ES6, the Boolean object is used to show two values either it may be true or false. In JavaScript, we can use the Boolean object as a...

4 minutes read.

String in ES6

String in ES6 The term string can be defined as an object used to represent the sequence of the characters. The string is widely used to store a text-based value like...

9 minutes read.

Immediately Invoked Function Expression ES6

Immediately Invoked Function Expression (IIFE) Immediately Invoked Function Expression (IIFE) is a JavaScript function used to ignore the variable hoisting from inside the code block. An IIFE is a function that runs...

3 minutes read.

Functions in ES6 | ECMAScript 6

Functions in ECMAScript 6 In ECMAScript programming language, the term ‘function’ can be defined as a collection or group of input statements, used to perform particular computational calculations and generate the output...

9 minutes read.

Operators in ES6 | ECMAScript 6

Operators in ECMAScript 6 The term Operator is refers to a symbol that is used to instruct the computer system to perform a specific operation. The ECMAScript 2015 supports a rich set of operators....

11 minutes read.

Object in ES6

Object in ES6 The term object can be defined as an instance that contains the group of key-value pairs. We can modify it using the life cycle of an object in...

6 minutes read.

Page Redirect in ES6

Page Redirect in ES6 The term redirect can be defined as a process to send the user and search engine on a specified URL from the original page. “The page redirection is...

3 minutes read.

Validation in ES6

Validation in ES6 The term validation can be defined as a mechanism to check the information given by the user is correct or not according to the requirements. If the data...

9 minutes read.

Set in ES6

Set in ES6 A Set is a kind of object used to create a group of unique values. The values stored in a set can be string, literal, array, and integer....

6 minutes read.

Template Literals in ES6

Template Literals in ES6            ES6 introduced a new feature called template literals. It facilitates us to define a multiline string and executes the string interpolation. The template literals are referred to...

3 minutes read.

Loop Control Statement in ES6

Loop Control Statement in ECMAScript 6 The loop control statements can be defined as a set of conditions that control the execution of the loop. It executes the loop until the condition becomes false....

6 minutes read.

Array Methods in ES6

Array Methods in ES 6 ES 6 introduced the various new methods such as Array,from(), Array,of(). Here, we have the various array methods used in JavaScript, they are given below in the table...

10 minutes read.

Variables in ES6 | ECMAScript 6

Variables in ECMAScript 6 A Variable is a place in memory used to store a value. There are some rules for declaration of the variable- Variable Initialization Initialization is a process of locating and storing the initial...

4 minutes read.