JavaScript map forEach() method is used to loop over the map. This method executes the specified function once for each key/value pair in the Map object.
forEach(callbackFn, thisArg)
JavaScript map forEach
Simple example code fetch the values from the Map object.
<!DOCTYPE html>
<html>
<body>
<script >
var map = new Map();
map.set(1,"jQuery");
map.set(2,"Angular JS");
map.set(3,"Bootstrap");
function display(values) {
console.log(values);
}
map.forEach(display);
</script>
</body>
</html>
Output:
Another example fetch the values and keys from the Map object.
<script >
var map = new Map();
map.set(1,"jQuery");
map.set(2,"Angular JS");
map.set(3,"Bootstrap");
function display(values, key) {
console.log(key,values);
}
map.forEach(display);
</script>
More Syntax
// Arrow function
forEach(() => { /* ... */ } )
forEach((value) => { /* ... */ } )
forEach((value, key) => { /* ... */ } )
forEach((value, key, map) => { /* ... */ } )
// Callback function
forEach(callbackFn)
forEach(callbackFn, thisArg)
// Inline callback function
forEach(function() { /* ... */ })
forEach(function(value) { /* ... */ })
forEach(function(value, key) { /* ... */ })
forEach(function(value, key, map) { /* ... */ })
forEach(function(value, key, map) { /* ... */ }, thisArg)
Do comment if you have any doubts or suggestions on this Js map topic.
Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.
OS: Windows 10
Code: HTML 5 Version