JavaScript Map keys()
- Previous Page has()
- Next Page set()
- Go to the Previous Level JavaScript Map Reference Manual
定义和用法
keys()
方法返回包含 Map 中所有键的迭代器对象。
keys()
方法不会改变原始 Map。
实例
例子 1
// 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; }
Object as key
Note:The ability to use objects as keys is an important feature of Map.
Example 2
// 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);
Remember:The key is an object (apples), not a string ("apples"):
Example 3
fruits.get("apples"); // Returns undefined
Syntax
map.keys()
Parameters
None.
Return value
Type | Description |
---|---|
Iterator | An iterable object containing all keys of the Map. |
Browser support
map.keys()
Is a feature of ECMAScript6 (ES6).
Starting from June 2017, all modern browsers support ES6 (JavaScript 2015):
Chrome | Edge | Firefox | Safari | Opera |
---|---|---|---|---|
Chrome 51 | Edge 15 | Firefox 54 | Safari 10 | Opera 38 |
May 2016 | April 2017 | June 2017 | September 2016 | June 2016 |
map.keys()
Not Supported in Internet Explorer.
- Previous Page has()
- Next Page set()
- Go to the Previous Level JavaScript Map Reference Manual