JS: Map.prototype.keys

By Xah Lee. Date: . Last updated: .

New in JS2015.

mapObj.keys()
Return a Iterable Object (also is a Iterator) for the keys in the map instance mapObj.
const xx = new Map([[3, "n3"], [4, "n4"], [5, "n5"]]);

for (let k of xx.keys()) console.log(k);
// 3
// 4
// 5

for (let [k, v] of xx) console.log(k, v);
// 3 n3
// 4 n4
// 5 n5

This example shows the result is iterator and iterable:

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

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

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

// is also a iterable
console.log(
  Symbol.iterator in (xx.keys()),
);
BUY ΣJS JavaScript in Depth