The Wayback Machine - https://web.archive.org/web/20240201203225/https://www.geeksforgeeks.org/how-to-iterate-over-map-elements-in-javascript/
Open In App
Related Articles

How to iterate over Map elements in JavaScript ?

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

Map() is a collection of elements in JavaScript where each element is kept as a Key and value pair. This method applies a callback function as a parameter to each element of the array before returning the updated array.

The Map() keyword is used to generate the object. The map() method loops through each object in the given array. The map() method combines with the entries() method to loop through the object and returns an array containing key-value pairs as its data.

Here we have two methods to iterate Map element in JavaScript:

Approach 1: Using for…of loop

The for…of loop iterates over the iterable objects (like Array, Map, Set, arguments object, …, etc), invoking a custom iteration hook with statements to be executed for the value of each distinct property.

Syntax:

for ( variable of iterableObjectName) {
...
}

Example: In this example, we are using for…of the loop to iterate over the map object.

Javascript




let newMap = new Map()
 
newMap.set("Cricket", "sport");
newMap.set("Apple", "fruit");
 
for (let [key, value] of newMap) {
    console.log(key + " is " + value);
}


Output

Cricket is sport
Apple is fruit

Approach 2: Using forEach() loop

The Map.forEach() method is used to loop over the map with the given function and execute the given function over each key-value pair.

Syntax:

myMap.forEach(callback, value, key, thisArg)

Example: Here we show the basic use of the Javascript Map.forEach() method.

Javascript




// Creating a map using Map object
let newMap = new Map();
 
// Adding values to the map
newMap.set("Monday", 1);
newMap.set("Tuesday", 2);
newMap.set("Wednesday", 3);
 
// Logging map object to console
newMap.forEach((values, keys) => {
    console.log(values, keys);
});


Output

1 Monday
2 Tuesday
3 Wednesday

Approach 3: Using Map.keys() method

In this Approach, we are using map.keys() method for iteration of the given map. the key() method returns the value and the result. This returns the iterator object that contains keys in the map.

Example: In this example, we are using map.keys() method for iteration of the given map.

Javascript




let myMap = new Map([['a', 1],
    ['b', 3], ['d', 10]])
let keys = myMap.keys();
 
while (true) {
    let result = keys.next();
    if (result.done) break;
    console.log(result.value);
}


Output

a
b
d

Learn to code easily with our course Coding for Everyone. This course is accessible and designed for everyone, even if you're new to coding. Start today and join millions on a journey to improve your skills!

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Looking for a place to share your ideas, learn, and connect? Our Community portal is just the spot! Come join us and see what all the buzz is about!


Last Updated : 07 Dec, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads
Complete Tutorials