翻译正在进行中。

set() 方法为 Map 对象添加或更新一个指定了键(key)和值(value)的(新)键值对。

语法

myMap.set(key, value);

参数

key
要添加至相应 Map 对象的元素的键。
value
要添加至相应 Map 对象的元素的值。

返回值

Map 对象

示例

使用 set 方法

var myMap = new Map();

// 将一个新元素添加到 Map 对象
myMap.set("bar", "foo");
myMap.set(1, "foobar");

// 在Map对象中更新某个元素的值
myMap.set("bar", "baz");

Using the set method with chaining

Since the set() method returns back the same Map object, you can chain the method call like below:

// Add new elements to the map with chaining. 
myMap.set('bar', 'foo')
     .set(1, 'foobar')
     .set(2, 'baz');

规范

规范 状态 备注
ECMAScript 2015 (6th Edition, ECMA-262)
Map.prototype.set
Standard Initial definition.
ECMAScript Latest Draft (ECMA-262)
Map.prototype.set
Draft  

浏览器兼容性

Update compatibility data on GitHub
DesktopMobileServer
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidEdge MobileFirefox for AndroidOpera for AndroidSafari on iOSSamsung InternetNode.js
Basic supportChrome Full support 38Edge Full support 12Firefox Full support 13IE Partial support 11
Notes
Partial support 11
Notes
Notes Returns 'undefined' instead of the 'Map' object.
Opera Full support 25Safari Full support 8WebView Android Full support 38Chrome Android Full support 38Edge Mobile Full support 12Firefox Android Full support 14Opera Android Full support 25Safari iOS Full support 8Samsung Internet Android Full support Yesnodejs Full support Yes

Legend

Full support  
Full support
Partial support  
Partial support
See implementation notes.
See implementation notes.

参见

文档标签和贡献者

此页面的贡献者: mdnwebdocs-bot, RainSlide, MaZheng, Hushabyme, zachary05
最后编辑者: mdnwebdocs-bot,