Map.prototype.forEach
Calls a function for each entry of a Map.
Available since: ES2015
Syntax
map.forEach(callbackFn)Parameters
| Parameter | Type | Description |
|---|---|---|
| callbackFn | Function | A function that receives the value, the key and the Map. |
Return value
undefinedReturns nothing.
Examples
Note the order of the arguments:
const ages = new Map([['Anna', 25], ['Oleg', 31]]);
ages.forEach((age, name) => {
console.log(name, age);
});Output:
Anna 25
Oleg 31Notes
The value comes first and the key second, like an array's forEach() (element, index). A for...of loop with destructuring reads more clearly.
See also
- Map.prototype.entries — Returns the key–value pairs of a Map.
- Array.prototype.forEach — Runs a function for every element of an array.
- for...of — Loops over the values of arrays, strings, Maps, Sets and other iterables.
In the tutorial
- Map and Set — The Map and Set collections in JavaScript: a dictionary with keys of any type and a set of unique values.