JavaScript Map forEach()
Syntax & Examples
Map.forEach() method
The forEach() method of the Map object in JavaScript calls a provided callback function once for each key-value pair present in the Map object, in insertion order. If a thisArg parameter is provided to forEach, it will be used as the this value for each callback.
Syntax of Map.forEach()
There are 2 variations for the syntax of Map.forEach() method. They are:
forEach(callbackFn)
Parameters
Parameter | Optional/Required | Description |
---|---|---|
callbackFn | required | Function to execute for each element, taking three arguments: value, key, and the Map object being traversed. |
This method calls callbackFn once for each key-value pair present in the Map object, in insertion order.
Returns value of type undefined
.
forEach(callbackFn, thisArg)
Parameters
Parameter | Optional/Required | Description |
---|---|---|
callbackFn | required | Function to execute for each element, taking three arguments: value, key, and the Map object being traversed. |
thisArg | optional | Value to use as this when executing callbackFn. |
This method calls callbackFn once for each key-value pair present in the Map object, in insertion order. thisArg
parameter will be used as the this value for each callback.
Returns value of type undefined
.
✐ Examples
1 Using forEach() to log key-value pairs
In JavaScript, we can use the forEach()
method to log each key-value pair in a Map object.
For example,
- Create a new Map object
map
with initial key-value pairs. - Use the
forEach()
method to log each key-value pair in themap
to the console.
JavaScript Program
const map = new Map([['a', 1], ['b', 2], ['c', 3]]);
map.forEach((value, key) => {
console.log(`${key}: ${value}`);
});
Output
a: 1 b: 2 c: 3
2 Using forEach() with a thisArg parameter
In JavaScript, we can use the forEach()
method with a thisArg parameter to log each key-value pair in a Map object with a specific this context.
For example,
- Create a new Map object
map
with initial key-value pairs. - Create an object
logger
with aprefix
property. - Use the
forEach()
method with a callback function that logs each key-value pair in themap
to the console, usinglogger
as the this context.
JavaScript Program
const map = new Map([['a', 1], ['b', 2], ['c', 3]]);
const logger = { prefix: 'Key-Value Pair:' };
map.forEach(function(value, key) {
console.log(`${this.prefix} ${key}: ${value}`);
}, logger);
Output
Key-Value Pair: a: 1 Key-Value Pair: b: 2 Key-Value Pair: c: 3
3 Using forEach() to modify values in a Map
In JavaScript, we can use the forEach()
method to modify each value in a Map object.
For example,
- Create a new Map object
map
with initial key-value pairs. - Use the
forEach()
method to multiply each value in themap
by 2. - Log the modified
map
to the console.
JavaScript Program
const map = new Map([['a', 1], ['b', 2], ['c', 3]]);
map.forEach((value, key, map) => {
map.set(key, value * 2);
});
console.log(map);
Output
Map { 'a' => 2, 'b' => 4, 'c' => 6 }
Summary
In this JavaScript tutorial, we learned about forEach() method of Map: the syntax and few working examples with output and detailed explanation for each example.