JS: Map.prototype.forEach

By Xah Lee. Date: . Last updated: .

New in ES2015.

Apply function f to all entries in map map_obj in the order the entries are inserted. Does not modify the map object. Return undefined.

If thisArg is given, it will be used as this value of f. If it is not given, undefined is used.

[see JS: “this” Binding]

The function f is passed 3 arguments: value, key, map_object.

const m = new Map( [["a", 1], ["b",2] ]);

// apply a function to each map entry
const rr = m.forEach( (v,k) => { console.log(v, k); } );

// prints
// 1 'a'
// 2 'b'

// does not modify the variable
console.log (m); // Map { 'a' => 1, 'b' => 2 }

// returns undefined
console.log( rr === undefined ); // true

See also: JS: Iterate Over Map Object

Reference

ECMAScript 2015 §Keyed Collection#sec-map.prototype.foreach

JS Map Topic

  1. the Map Object Tutorial
  2. Map Object
  3. Map.prototype
  4. Iterate Over Map Object
  5. Convert Object to/from Map
  6. Map Filter
Liket it? Put $5 at patreon.

Or, Buy JavaScript in Depth

If you have a question, put $5 at patreon and message me.

Web Dev Tutorials

  1. HTML
  2. Visual CSS
  3. JS in Depth
  4. JS Reference
  5. DOM
  6. SVG
  7. Web Dev Blog