3

Map 结构转为数组结构

let map = new Map([
        [1,'one'],
        [2,'two'],
        [3,'three']
      ])
      let a = [...map.keys()]
      console.log(a)//[1,2,3]
      
      let a = [...map.values()]
      console.log(a)//["one", "two", "three"]

Map 循环遍历
Map 原生提供三个遍历器:

keys():返回键名的遍历器。
values():返回键值的遍历器。
entries():返回所有成员的遍历器。

var  arr = [['b', 'x'], [2, 'y'], [3, 'z']]
    var m = new Map(arr);
      m.forEach((value,key,map)=>{
        console.log(value)
        console.log(key)
        console.log(map)
      })

可以和set一起使用构建一个map对象

var myMap = new Map()
      myMap.set('a','a')
      myMap.set('b','b')
      myMap.set('c','c')
      console.log(myMap)//{"a" => "a" ,"b" => "b","c" => "c"}

HappyCodingTop
526 声望847 粉丝

Talk is cheap, show the code!!