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


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

Map Topic

  1. JS: the Map Object Tutorial
  2. JS: Map Object
  3. JS: Map.prototype
  4. JS: Iterate Over Map Object
  5. JS: Convert Object to/from Map
  6. JS: 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.