JS Built-ins
← Map52 / 55
MapforEachbeginner

Map forEach() β€” collect keys

πŸ“– 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()

The second argument to the callback is the key.

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

πŸ’‘ Hints (1)
  • β€’ forEach callback: first arg is value, second is key