JS map get()
get()
方法返回某个 Map
对象中的一个指定元素。
The source for this interactive example is stored in a GitHub repository. If you’d like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.
语法
myMap.get(key);
参数
key
必须参数,也是它唯一的参数,要从目标 Map
对象中获取的元素的键。
返回值
返回一个 Map
对象中与指定键相关联的值,如果找不到这个键则返回 undefined
。
示例
使用 get
方法
var myMap = new Map();
myMap.set("bar", "foo");
myMap.get("bar"); // 返回 "foo"
myMap.get("baz"); // 返回 undefined
规范
规范 | 状态 | 备注 |
---|---|---|
ECMAScript 2015 (6th Edition, ECMA-262)Map.prototype.get | Standard | Initial definition. |
ECMAScript Latest Draft (ECMA-262)Map.prototype.get | Draft |