一行代码实现 数组去重

// 一行代码去重数组

const uniqueList = [...new Set( [1, 1, 2,26,2,2,2,2,2,2, 3, 6, 45, 8, 5, 4, 6, 5] )] 

打印出来看一下结果
console.log(“uniqueList”,uniqueList)

结果如下:
在这里插入图片描述

原理: ES6 中,引入了一个新的数据结构类型:Set。而 Set 与 Array 的结构是很类似的,且 Set 和 Array 可以相互进行转换。区别是不存value 只是存储 key key的集合,可以放进去多个相同的key,但是只会有一个key起作用

发布了37 篇原创文章 · 获赞 20 · 访问量 6744

猜你喜欢

转载自blog.csdn.net/qq_39051175/article/details/100153265