alicelmx 2019-11-03
const peoples = new Map() peoples.set(‘name‘, ‘L‘) peoples.set(‘age‘, ‘25‘) peoples.set(‘sex‘, ‘男‘) console.log(peoples); // {"name" => "L", "age" => "25", "sex" => "男"}
1 for (let key of peoples) { 2 console.log(key); // ["name", "L"] ["age", "25"] ["sex", "男"] 3 } // 2. 用forEach遍历 peoples.forEach((value, key, map) => { console.log(value, key, map); }); // 输出:L name Map(3) {"name" => "L", "age" => "25", "sex" => "男"} // 25 age Map(3) {"name" => "L", "age" => "25", "sex" => "男"} // 男 sex Map(3) {"name" => "L", "age" => "25", "sex" => "男"} // Map数据转换成数组 使用扩展运算符(...) var arr = [...peoples] console.log(arr); // 0: (2)["name", "L"] // 1: (2)["age", "25"] // 2: (2)["sex", "男"]