当前位置:网站首页>ES6数组去重的三个简单办法
ES6数组去重的三个简单办法
2022-07-01 18:36:00 【全栈程序员站长】
大家好,又见面了,我是你们的朋友全栈君。
ES6数组去重的三个简单办法
简单说一下利用ES6实现数组去重的三个办法。
第一种: 利用Map对象和数组的filter方法
贴上相关代码
打印后的结果
通过打印我们发现,确实实现了我们想要的效果。那么下面简单来解释一下。 1.Map对象是ES6提供的一个新的数据结构,其中has的办法是返回一个布尔值,表示某个值是否存在当前的Mp对象之中,set的办法是给Map对象设置key/value。 2.filter() 方法创建一个新的数组,新数组中的元素是通过检查指定数组中符合条件的所有元素。 所以说,Map对象结合filter方法可以达到数组去重的效果~
第二种:利用Set对象和数组的Array.from方法
同样贴上相关代码片段
打印运行后的结果
简单来说,第二种方法比第一种还简单。同样来简单解释一下。 1.Set是ES6新提供的数据结构,类似于数组,但是本身没有重复值。 2.Array.from方法用于将两类对象转为真正的数组:类似数组的对象(array-like object)和可遍历(iterable)的对象(包括 ES6 新增的数据结构 Set 和 Map)。 所以set结合Array.from同样可以达到数组去重的效果。不过需要注意的是,主流浏览器像Chrome,Firfox,Opera,Safari,包括微软的Edge,都是支持的,但是唯独IE系列不支持。
第三种:利用Set+扩展运算符 …
第三种办法可以说是更简单 贴上相关代码
打印后运行的结果
这就是利用ES6新特性达到数组去重的三种办法,这三种办法有个共同的好处就是代码简洁,对于undefined和NaN也同样可以达到去重的效果~~
如果你有其他办法也欢迎一起分享一下~
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/130837.html原文链接:https://javaforall.cn
边栏推荐
- Go语言自学系列 | go语言数据类型
- 解决方案:可以ping别人,但是别人不能ping我
- R language ggplot2 visualization: gganimate creates a dynamic histogram animation (GIF), and displays the histogram and enter step by step along a given dimension in the animation_ Growth function and
- 搭建一個通用監控告警平臺,架構上需要有哪些設計
- 每周推荐短视频:警惕“现象”与“问题”相互混淆
- Privacy sandbox is finally coming
- What if the reliability coefficient is low? How to calculate the reliability coefficient?
- Depth first search - DFS (burst search)
- C# SelfHost WebAPI (2)
- 用WPF写一款开源方便、快捷的数据库文档查询、生成工具
猜你喜欢

如何在自有APP内实现小程序实现连麦直播

主成分之综合竞争力案例分析
![[source code analysis] NVIDIA hugectr, GPU version parameter server - (1)](/img/92/f3a70e7086aeedf41eea3eef98b5aa.jpg)
[source code analysis] NVIDIA hugectr, GPU version parameter server - (1)

Implement a Prometheus exporter

Lumiprobe biomolecular quantification - qudye Protein Quantification Kit

Livedata postvalue will "lose" data

Bernoulli distribution (a discrete distribution)

docker 部署mysql8.0

Leetcode-160相交链表

Technology implementation and Architecture Practice
随机推荐
Sanfeng cloud 0215 I often use
[today in history] February 15: Pascal's father was born; YouTube was founded; Kotlin language comes out
C# SelfHost WebAPI (2)
解决方案:可以ping别人,但是别人不能ping我
Bernoulli distribution (a discrete distribution)
Lumiprobe lumizol RNA extraction reagent solution
How to change guns for 2D characters
Roll out! Enlightenment!
Must see, time series analysis
PriorityQueue的用法和底层实现原理
Implementation of converting PCM file to WAV
如何运营好技术相关的自媒体?
Lumiprobe非荧光炔烃丨EU(5-乙炔基尿苷)
关于企业中台规划和 IT 架构微服务转型
11、用户、组和权限(1)
A database editing gadget that can edit SQLite database. SQLite database replaces fields. SQL replaces all values of a field in the database
记一次 .NET 差旅管理后台 CPU 爆高分析
Force buckle day33
app发版后的缓存问题
实现一个Prometheus exporter