JS: Map.prototype.keys

By Xah Lee. Date: . Last updated: .

New in ES2015.

map_obj.keys()

Return a iterator (also is a iterable) for the keys in the map instance map_obj.

[see JS: Iterator]

const mm = new Map([[3, "n3"], [4, "n4"], [5, "n5"] ]);

for (let k of mm.keys()) { console.log(k) }
// prints
// 3
// 4
// 5

for (let [k,v] of mm) { console.log(k) }
// prints
// 3
// 4
// 5

This example shows the result is iterator and iterable:

const mp = new Map( [[1,2], [3,4]]);

console.log ( mp.keys ); // [Function: keys]
// result is a function that returns a iterator object

// call it
console.log ( mp.keys() ); // MapIterator { 1, 3 }
// result is a iterator object

// is also a iterable
console.log (
 Symbol.iterator in ( mp.keys() )
); // true

Reference

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

JS Map Topic

  1. the Map Object Tutorial
  2. Map Object
  3. Map.prototype
  4. Iterate Over Map Object
  5. Convert Object to/from Map
  6. Map Filter
Liket it? Put $5 at patreon.

Or, Buy JavaScript in Depth

If you have a question, put $5 at patreon and message me.

Web Dev Tutorials

  1. HTML
  2. Visual CSS
  3. JS in Depth
  4. JS Reference
  5. DOM
  6. SVG
  7. Web Dev Blog