Javascript Map forEach()

Introduction

The Javascript Map forEach() method runs a function on each key/value pair in the Map object.

It will run in insertion order for the Map object.

myMap.forEach(callback[, thisArg])
  • callback - function to execute for each element.
  • thisArg - value to use as this when executing callback.

The callback() function has three arguments:

  • the element's value
  • the element key
  • the Map object itself

Printing the contents of a Map object

function logMapElements(value, key, map) {
    console.log(`map.get('${key}') = ${value}`)
}
let m = new Map([['foo', 3], ['bar', {}], ['baz', undefined]]);

m.forEach(logMapElements);/*from  w ww  . j a  v a  2s . c om*/



PreviousNext

Related