Koda Sifawa Hanyar Map

new Map() 方法

可以通过将数组传递给 new Map() 构造函数来创建 Map:

实例

// 创建一个 Map
const fruits = new Map([
  ["apples", 500],
  ["bananas", 300],
  ["oranges", 200]
]);

Try it yourself

Map.get()

可以使用 get() 方法获取 Map 中键的值:

实例

fruits.get("apples");

Try it yourself

Map.set()

可以使用 set() 方法向 Map 添加元素:

实例

// 创建一个 Map
const fruits = new Map();
// 设置 Map 值
fruits.set("apples", 500);
fruits.set("bananas", 300);
fruits.set("oranges", 200);

Try it yourself

set() 方法也可以用于更改现有的 Map 值:

实例

fruits.set("apples", 500);

Try it yourself

Map.size

size 属性返回 Map 中元素的数量:

实例

fruits.size;

Try it yourself

Map.delete()

delete() 方法移除 Map 中的一个元素:

实例

fruits.delete("apples");

Try it yourself

Map.clear()

clear() 方法移除 Map 中的所有元素:

实例

fruits.clear();

Try it yourself

Map.has()

如果 Map 中存在某个键,has() 方法返回 true:

实例

fruits.has("apples");

Try it yourself

尝试以下代码:

fruits.delete("apples");
fruits.has("apples");

Try it yourself

Map.forEach()

forEach() 方法为 Map 中的每个键值对调用回调函数:

实例

// 列出所有条目
let text = "";
fruits.forEach(function(value, key) {
  text += key + ' = ' + value;
});

Try it yourself

Map.entries()

entries() 方法返回一个包含 Map 中 [key, value] 对的迭代器对象:

实例

// 列出所有条目
let text = "";
for (const x of fruits.entries()) {
  text += x;
}

Try it yourself

Map.keys()

keys() 方法返回一个包含 Map 中键的迭代器对象:

实例

// 列出所有键
let text = "";
for (const x of fruits.keys()) {
  text += x;
}

Try it yourself

Map.values()

values() 方法返回一个包含 Map 中值的迭代器对象:

实例

// 列出所有值
let text = "";
for (const x of fruits.values()) {
  text += x;
}

Try it yourself

可以使用 values() 方法对 Map 中的值求和:

实例

// 对所有值求和
let total = 0;
for (const x of fruits.values()) {
  total += x;
}

Try it yourself

对象作为键

提示:能够使用对象作为键是 Map 的一个重要特性。

实例

// 创建对象
const apples = {name: 'Apples'};
const bananas = {name: 'Bananas'};
const oranges = {name: 'Oranges'};
// 创建一个 Map
const fruits = new Map();
// 向 Map 添加新元素
fruits.set(apples, 500);
fruits.set(bananas, 300);
fruits.set(oranges, 200);

Try it yourself

注意:键是对象(apples),而不是字符串("apples"):

实例

fruits.get("apples"); // 返回 undefined

Try it yourself

JavaScript Map.groupBy()

ES2024 向 JavaScript 添加了 Map.groupBy() 方法。

Map.groupBy() 方法根据回调函数返回的字符串值对对象的元素进行分组。

Map.groupBy() 方法不会更改原始对象。

实例

// 创建一个数组
const fruits = [
  {name: "apples", quantity: 300},
  {name: "bananas", quantity: 500},
  {name: "oranges", quantity: 200},
  {name: "kiwi", quantity: 150}
];
// 用于分组的回调函数
function myCallback({ quantity }) {
  return quantity > 200 ? "ok" : "low";
}
// 按数量分组
const result = Map.groupBy(fruits, myCallback);

Try it yourself

Browser support

Map.groupBy() It is an ES2024 feature.

From March 2024, all new browsers support this feature:

Chrome Edge Firefox Safari Opera
Chrome 117 Edge 117 Firefox 119 Safari 17.4 Opera 103
Satumba 2023 Satumba 2023 Oktoba 2023 Oktoba 2024 Mayu 2023

Warning:

ES2024 features are relatively new.

Old browsers may need alternative code (Polyfill).

Differences between Object.groupBy() and Map.groupBy()

Object.groupBy() da Map.groupBy() Differences are:

Object.groupBy() Zana tashi a cikin JavaScript objek.

Map.groupBy() Zana tashi a cikin Map objek.

Kikayi kwallaye na kwallaye na Map

Don sauki kikayi kwallaye, za a yanar:JavaScript Map kikayi.

Kikayi kanawa da bayanin da suka amfani wa Map kuma suka kira.