es6提供了Map数据结构,它类似于对象,也是键值对的集合,但是“键”的范围不限于字符串,各种类型的值(包括对象)都可以被当做键。Map也实现了iterator接口,可以用扩展运算符和for..of 遍历。
let map = new Map();
map.set('first', '娟');
map.set('last', 'yang');
console.log(map); //{"first" => "娟", "last" => "yang"}
console.log(map.size); //2
console.log(map.get('first')); //娟
console.log(map.has('first')); //true
// map.clear();
console.log(map);
console.log(...map); //["first", "娟"] ["last", "yang"]
for (let v of map) {
console.log(v); //["first", "娟"] ["last", "yang"]
}
for (let [key] of map) {
console.log(key); //first last
}
for (let [, value] of map) {
console.log(value); //娟 yang
}

运行结果