JS Built-ins
← Map51 / 55
MapforEachbeginner

Map forEach() β€” collect values

πŸ“– Usage Example
const m = new Map([['a', 1], ['b', 2]])
m.forEach((val, key) => console.log(key, val))
// 'a' 1, then 'b' 2

Use Map.forEach() to execute a callback for each key-value pair in the map.

Map.prototype.forEach()

forEach() calls a callback for each entry. The callback receives (value, key, map).

Challenge: Use forEach to collect all values into an array.

πŸ’‘ Hints (1)
  • β€’ forEach callback signature is (value, key, map)