set 方法 (Map)
向映射添加新元素。
语法
mapObj.set(key, value)
参数
mapObj
必要参数。Map对象。
key
必要参数。新元素的键。
value
必要参数。要添加的元素的值。
属性值、返回值
返回包含新键值对的Map对象。
备注
如果使用现有的键向集合添加值,则新值会替换旧值。
示例
下面的示例演示如何将成员添加到Map,然后检索成员。
var m = new Map(); m.set(1, "black"); m.set(2, "red"); m.set("colors", 2); m.forEach(function (item) { document.write(item.toString() + "<br />"); }); // Output: // black // red // 2
必备条件
在Internet Explorer 11标准文档模式下支持此项。此外,也在应用商店应用(Windows 8.1和Windows Phone 8.1)中受支持。请参阅版本信息。
在以下文档模式中不受支持:怪异模式、Internet Explorer 6标准模式、Internet Explorer 7标准模式、Internet Explorer 8标准模式、Internet Explorer 9标准模式和Internet Explorer 10标准模式。在Windows 8中不受支持。