How do you iterate over a Map object in JavaScript?

In JavaScript, a Map object is a collection of key-value pairs where each key can be of any type.

The keys in a Map object are unique, and the values can be of any type. Iterating over a Map object can be done in various ways depending on the requirements.

Here are some examples of iteration over a Map object:

Method 1: Using the for...of loop:

The for...of loop is a convenient way to iterate over a Map object. It allows you to iterate over the keys or the values or both.

For example:

let myMap = new Map();

myMap.set("key1", "value1");
myMap.set("key2", "value2");
myMap.set("key3", "value3");

// Iterate over the keys
for (let key of myMap.keys()) {
  console.log(key);
}

// Iterate over the values
for (let value of myMap.values()) {
  console.log(value);
}

// Iterate over both keys and values
for (let entry of myMap) {
  console.log(entry[0], entry[1]);
}

Output:

key1
key2
key3
value1
value2
value3
key1 value1
key2 value2
key3 value3

Method 2: Using the forEach() method:

The forEach() method is another way to iterate over a Map object. It allows you to execute a function for each key-value pair in the Map object.

For example:

let myMap = new Map();

myMap.set("key1", "value1");
myMap.set("key2", "value2");
myMap.set("key3", "value3");

// Iterate over the key-value pairs
myMap.forEach(function (value, key) {
  console.log(key, value);
});

Output:

key1 value1
key2 value2
key3 value3

Method 3: Using the entries() method and a while loop:

The entries() method returns an iterator object that contains the key-value pairs of the Map object. You can iterate over this iterator object using a while loop.

For example:

let myMap = new Map();

myMap.set("key1", "value1");
myMap.set("key2", "value2");
myMap.set("key3", "value3");

// Get the iterator object for the Map object
let entries = myMap.entries();

// Iterate over the key-value pairs using a while loop
let currentEntry = entries.next();
while (!currentEntry.done) {
  console.log(currentEntry.value[0], currentEntry.value[1]);
  currentEntry = entries.next();
}

Output:

key1 value1
key2 value2
key3 value3

Method 4: Using the Array.from() method:

The Array.from() method can be used to create a new array from an iterable object, including a Map object. This new array can then be iterated over using a for...of loop.

For example:

let myMap = new Map();

myMap.set("key1", "value1");
myMap.set("key2", "value2");
myMap.set("key3", "value3");

// Create a new array from the Map object
let entriesArray = Array.from(myMap);

// Iterate over the key-value pairs in the array
for (let [key, value] of entriesArray) {
  console.log(key, value);
}

Output:

key1 value1
key2 value2
key3 value3

Method 5: Using the spread operator

The spread operator (...) can also be used to create a new array from a Map object. This new array can then be iterated over using a for...of loop.

For example:

let myMap = new Map();

myMap.set("key1", "value1");
myMap.set("key2", "value2");
myMap.set("key3", "value3");

// Create a new array from the Map object using the spread operator
let entriesArray = [...myMap];

// Iterate over the key-value pairs in the array
for (let [key, value] of entriesArray) {
  console.log(key, value);
}

Output:

key1 value1
key2 value2
key3 value3

Method 6: Using the Object.fromEntries() method:

The Object.fromEntries() method can be used to create a new object from an iterable of key-value pairs. This iterable can be a Map object, which means that you can iterate over a Map object and create a new object from it using this method.

For example:

let myMap = new Map();

myMap.set("key1", "value1");
myMap.set("key2", "value2");
myMap.set("key3", "value3");

// Create a new object from the Map object using the Object.fromEntries() method
let entriesObject = Object.fromEntries(myMap);

// Iterate over the key-value pairs in the object
for (let key in entriesObject) {
  console.log(key, entriesObject[key]);
}

Output:

key1 value1
key2 value2
key3 value3

Thank you for reading, and let’s have conversation with each other

Thank you for reading my article. Let’s have conversation on Twitter and LinkedIn by connecting.

Read more: