编辑代码

//JSRUN引擎2.0,支持多达30种语言在线运行,全仿真在线交互输入输出。 

// map转数组 使用... 展开符
const map = new Map ([
    [1,'foo'],
    [2,'bar'],
    [3,'baz']
])

const arr = [...map]
console.log(arr)

// 数组转map   通过数组map方法,转换数据结构
const list = ['foo','bar','baz']
const listMap = new Map(list.map((value,key)=>[key,value]))
    
console.log(listMap)

// map转对象  遍历map对象
function map2Object(map){
    const obj = {}
    for(let [key,value] of map){
        obj[key] = value
    }
    return obj
}

const obj = map2Object(map)

console.log(obj)

// 对象转map
// (1) 遍历对象的key
function object2Map(obj){
  const map = new Map()
  for(let  key in obj){
      map.set(key,obj[key])
  }
  return map
}

const objMap = object2Map(obj)
console.log(objMap)
//(2) 通过Object.entries(obj)
const objMap2 = new Map(Object.entries(obj))
console.log(objMap2)