Map.prototype.set()
Baseline Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
set()
メソッドは、指定されたキーと値を持つ要素を Map
オブジェクトに追加したり、更新したりします。
試してみましょう
const map1 = new Map();
map1.set("bar", "foo");
console.log(map1.get("bar"));
// Expected output: "foo"
console.log(map1.get("baz"));
// Expected output: undefined
構文
js
set(key, value)
引数
key
-
Map
オブジェクトに追加する要素のキーです。キーはいずれかの JavaScript 型 (いずれかのプリミティブ値または任意の型の JavaScript オブジェクト)です。 value
-
Map
オブジェクトに追加する要素の値です。値はいずれかの JavaScript 型 (いずれかのプリミティブ値または任意の型の JavaScript オブジェクト)です。
返値
Map
オブジェクトです。
例
set() の使用
js
let myMap = new Map();
// マップに新しい要素を追加する
myMap.set("bar", "foo");
myMap.set(1, "foobar");
// マップにある要素を更新する
myMap.set("bar", "baz");
set() のメソッドチェーンの使用
set()
メソッドは Map
オブジェクトを返すため、以下のようにメソッドチェーンを作ることができます。
js
// チェーンを使用してマップに新しい要素を追加する
myMap.set("bar", "foo").set(1, "foobar").set(2, "baz");
仕様書
Specification |
---|
ECMAScript® 2025 Language Specification # sec-map.prototype.set |
ブラウザーの互換性
Report problems with this compatibility data on GitHubdesktop | mobile | server | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
set |
Legend
Tip: you can click/tap on a cell for more information.
- Full support
- Full support
The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.