JS: Map.prototype.keys

By Xah Lee. Date: . Last updated: .

New in ES2015.

map_obj.keys()

Returns 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

Map Topic

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

Or, Buy JavaScript in Depth

Ask me question on patreon