Sunday, November 17, 2024
Google search engine
HomeLanguagesJavascriptJavaScript Map entries() Method

JavaScript Map entries() Method

JavaScript Map.entries() method is used for returning an iterator object which contains all the [key, value] pairs of each element of the map. It returns the [key, value] pairs of all the elements of a map in the order of their insertion. The Map.entries() method does not require any argument to be passed and returns an iterator object of the map. 

Syntax:

mapObj.entries()

Parameters: 

  • It does not require any parameters to be passed.

Return: 

  • The Map.entries() method returns the [key, value] pairs of all the elements of a map in the order of their insertion.

Examples of the above method are provided below. 

Example 1: In this example, a map object “myMap” has been created with three [key, value] pairs, and an iterator object “iterator_obj” method is created which uses Map. entries() method to return the [key, value] pairs of all the elements of a map in the order of their insertion. 

javascript




// creating a map object
let myMap = new Map();
 
// Adding [key, value] pair to the map
myMap.set(0, 'neveropen');
myMap.set(1, 'is an online portal');
myMap.set(2, 'for neveropen');
 
// creating an iterator object using Map.entries() method
let iterator_obj = myMap.entries();
 
// displaying the [key, value] pairs of all the elements of the map
console.log(iterator_obj.next().value);
console.log(iterator_obj.next().value);
console.log(iterator_obj.next().value);


Output:

Array [0, "neveropen"]
Array [1, "is an online portal"]
Array [2, "for neveropen"]

Example 2: In this example, a map object “myMap” has been created with three [key, value] pairs, and an iterator object “iterator_obj” method is created which uses Map. entries() method to return the [key, value] pairs of all the elements of a map in the order of their insertion. 

javascript




// creating a map object
let myMap = new Map();
 
// Adding [key, value] pair to the map
myMap.set(0, 'Maps');
myMap.set(1, 'in JavaScript');
 
// creating an iterator object using Map.entries() method
let iterator_obj = myMap.entries();
 
// displaying the [key, value] pairs of all the elements of the map
console.log(iterator_obj.next().value);
console.log(iterator_obj.next().value);


Output:

Array [0, "Maps"]
Array [1, "in JavaScript"]

Applications: Whenever we want to get all the [key, value] pairs of each element of a map using an iterator object, we use the Map.entries() method. 

Exceptions:

  • If the variable is not of the Map type then the Map.entries() operation throws a TypeError.
  • If the “iterator_obj.next().value” is used more times as compared to [key, value] pairs of a map, the Map.entries() method returns undefined for all those cases.

To see the difference between the Javascript Map and Objects, go through this Map vs Object In Javascript article.

We have a complete list of Javascript Map methods, to check those please go through this JavaScript Map Complete Reference article.

Supported Browsers:

  • Google Chrome 38 and above
  • Edge 12 and above
  • Firefox 20 and above
  • Opera 25 and above
  • Safari 8 and above

RELATED ARTICLES

Most Popular

Recent Comments