JavaScript: 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 JavaScript: Keyword “this”

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: JavaScript: Iterate Over Map Object

Reference

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

Map Topic

  1. JavaScript: the Map Object Tutorial
  2. JavaScript: Map Object
  3. JavaScript: Map.prototype
  4. JavaScript: Iterate Over Map Object
  5. JavaScript: Convert Object to Map Data Type
Like what you read? Buy JavaScript in Depth
or, buy a new keyboard, see Keyboard Reviews.