Src

Map

let recipeMap = new Map([
  ['cucumber', 500],
  ['tomatoes', 350],
  ['onion',    50]
]);
 
// 遍历所有的键(vegetables)
for (let vegetable of recipeMap.keys()) {
  alert(vegetable); // cucumber, tomatoes, onion
}
 
// 遍历所有的值(amounts)
for (let amount of recipeMap.values()) {
  alert(amount); // 500, 350, 50
}
 
// 遍历所有的实体 [key, value]
for (let entry of recipeMap) { // 与 recipeMap.entries() 相同
  alert(entry); // cucumber,500 (and so on)
}

JavaScript Object

Object.entries:从对象创建 Map

// 键值对 [key, value] 数组
let map = new Map([
  ["1", "str1"],
  [1, "num1"],
  [true, "bool1"],
]);
 
alert(map.get("1")); // str1
 
//or 
 
let obj = {
  name: "John",
  age: 30
};
 
let map = new Map(Object.entries(obj));
 
alert( map.get('name') ); // John

Object.fromEntries:从 Map 创建对象

let prices = Object.fromEntries([
  ['banana', 1],
  ['orange', 2],
  ['meat', 4]
]);
 
// 现在 prices = { banana: 1, orange: 2, meat: 4 }
 
alert(prices.orange); // 2
 
// or
 
let map = new Map();
map.set('banana', 1);
map.set('orange', 2);
map.set('meat', 4);
 
let obj = Object.fromEntries(map); // 省掉 .entries()
 
// 完成了!
// obj = { banana: 1, orange: 2, meat: 4 }
 
alert(obj.orange); // 2