es6之map和set整理

①es6做了集合类型的统一,Array,map,set都可以用for…of遍历
②map类似对象,但是他的key可以是字符串,对象类型。
③set类似数组,里面的值都是唯一不能重复。
看一下map的输出:

let map=new Map()
map.set(1,"hello")
map.get(1)
"hello"
let obj={}
map.set(obj,{name:"lisi"})
map.get(obj)
{name: "lisi"}

看一下set的输出:

let arr=[1,2,3,4,1,2]
console.log(new Set(arr))
VM1089:3 Set(4) {1, 2, 3, 4}

再把set类型转换成数组类型就可以达到数组去重的效果

let newArr=[...new Set(arr)]
console.log(newArr)
VM1369:2 (4) [1, 2, 3, 4]

这也是set的使用场景之一,map的使用场景参考:map使用场景

猜你喜欢

转载自blog.csdn.net/weixin_44494811/article/details/103305054