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. Returns 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.

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

// apply a function to each map entry
var t = 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(t); // undefined

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
Like what you read? Buy JavaScript in Depth