JS Built-ins
← Map36 / 55
Mapkeysbeginner

Map keys() β€” spread to array

πŸ“– Usage Example
const m = new Map([['a', 1], ['b', 2]])
for (const k of m.keys()) {
  console.log(k) // 'a', 'b'
}

Use Map.keys() to iterate over all keys in insertion order.

Map.prototype.keys()

keys() returns an iterator of the Map's keys in insertion order.

Challenge: Verify that spreading keys() gives the correct array.

πŸ’‘ Hints (1)
  • β€’ Spread the iterator with [...map.keys()] to get an array