The JavaScript map forEach() method is used to execute the specified function once for each key/value pair.
Syntax:
mapObj.forEach(callback)
mapObj.forEach(callback)
mapObj.forEach(callback)
Parameters:
callback: It refers to the function which has to be executed.
Example:
<!DOCTYPE html>
<html>
<body>
<script>
var hello=new Map();
hello.set(1,"GOOD MORNING");
hello.set(2,"GOOD AFTERNOON");
hello.set(3,"GOOD EVENING");
hello.set(4,"GOOD NIGHT");
document.writeln("Elements are:"+"<br>");
function print(elements)
{
document.writeln(elements+"<br>");
}
hello.forEach(print);
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<script>
var hello=new Map();
hello.set(1,"GOOD MORNING");
hello.set(2,"GOOD AFTERNOON");
hello.set(3,"GOOD EVENING");
hello.set(4,"GOOD NIGHT");
document.writeln("Elements are:"+"<br>");
function print(elements)
{
document.writeln(elements+"<br>");
}
hello.forEach(print);
</script>
</body>
</html>
<!DOCTYPE html> <html> <body> <script> var hello=new Map(); hello.set(1,"GOOD MORNING"); hello.set(2,"GOOD AFTERNOON"); hello.set(3,"GOOD EVENING"); hello.set(4,"GOOD NIGHT"); document.writeln("Elements are:"+"<br>"); function print(elements) { document.writeln(elements+"<br>"); } hello.forEach(print); </script> </body> </html>