let map = new Map() map.set('name', 'dust') map.set(function () {}, 'hello') map.set({}, 'hi') map.set(1, 'www.baidu.com') console.log(map)
运行结果:
let map2 = new Map([ ['name', 'dust'], [function () {}, 'hello'], ]) console.log(map2)
运行结果:
console.log(map.get('number'))
map.has()
console.log(map.has('number')) //true
map.clear() console.log(map) //Map(0) {}
map.keys()
for (const key of map.keys()) { console.log(key) }
map.values()
for (const value of map.values()) { console.log(value) }
for (const [key, value] of map.entries()) { console.log(key, value) }
forEach
遍历(value, key)
map.forEach((value, key) => { console.log(value, key) })
let map = new Map([ ['name', 'dust'], [function () {}, 'hello'], [{}, 'hi'], ['number', 99], ]) console.log(...map.entries())
运行结果:
[ 'name', 'dust' ] [ [Function (anonymous)], 'hello' ] [ {}, 'hi' ] [ 'number', 99 ]
执行以下代码:
let a = [...map] console.log(a)
运行结果:
此时你就可以类型转换了。