JavaScript Map keys()
Example
// Create a Map
const fruits = new Map([
["apples", 500],
["bananas", 300],
["oranges", 200]
]);
// List all Keys
let text = "";
for (const x of fruits.keys()) {
text += x;
}
Try it Yourself »
More Examples Below !
Description
The keys()
method returns an iterator object with the keys in a map:
The keys()
method does not change the original met.
Syntax
map.keys()
Parameters
NONE |
Return Value
Type | Description |
Iterator | An iterable object with the keys of the map. |
Objects as Keys
Note
Being able to use objects as keys is an important map feature.
Example
// Create Objects
const apples = {name: 'Apples'};
const bananas = {name: 'Bananas'};
const oranges = {name: 'Oranges'};
// Create a Map
const fruits = new Map();
// Add new Elements to the Map
fruits.set(apples, 500);
fruits.set(bananas, 300);
fruits.set(oranges, 200);
Try it Yourself »
Remember: The key is an object (apples), not a string ("apples"):
Browser Support
map.keys()
is an ECMAScript6 (ES6) feature.
ES6 (JavaScript 2015) is supported in all modern browsers since June 2017:
Chrome 51 | Edge 15 | Firefox 54 | Safari 10 | Opera 38 |
May 2016 | Apr 2017 | Jun 2017 | Sep 2016 | Jun 2016 |
map.keys()
is not supported in Internet Explorer.