当前位置:网站首页>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
边栏推荐
- Write an open source, convenient and fast database document query and generation tool with WPF
- code
- 力扣每日一题-第32天-589.N×树的前序遍历
- 宏观视角看抖音全生态
- 字节跳动数据平台技术揭秘:基于 ClickHouse 的复杂查询实现与优化
- The 13th simulation problem of the single chip microcomputer provincial competition of the Blue Bridge Cup
- 2020,最新手机号码手机验证正则表达式,持续更新
- [noip2015] jumping stone
- Regular expression
- 540. Single element in ordered array / 1684 Count the number of consistent strings
猜你喜欢

搭建一個通用監控告警平臺,架構上需要有哪些設計

Memo - about C # generating barcode

Mysql database of easyclick

Facebook聊单,SaleSmartly有妙招!

搭建一个通用监控告警平台,架构上需要有哪些设计

Leetcode-160 intersecting linked list

Write an open source, convenient and fast database document query and generation tool with WPF

Privacy sandbox is finally coming

Navicat Premium 15 永久破解和2021版本最新IDEA破解(亲测有效)

About enterprise middle office planning and it architecture microservice transformation
随机推荐
C language learning notes: type definition typedef and declaration external CSDN creation punch in
Excel之VBA简单宏编程
PriorityQueue的用法和底层实现原理
Sanfeng cloud 0215 I often use
[Chongqing Guangdong education] basic psychology reference materials of Tianjin Normal University
Implementation of converting PCM file to WAV
R language ggplot2 visualization: visualize the line graph and add customized Y-axis label information to the line graph using the labels function
Force buckle day33
Lumiprobe Lumizol RNA 提取试剂解决方案
宏观视角看抖音全生态
Case study on comprehensive competitiveness of principal components
ACM MM 2022视频理解挑战赛视频分类赛道冠军AutoX团队技术分享
Bernoulli distribution (a discrete distribution)
Static timing analysis (STA) in ic/fpga design
golang 错误处理
12种数据量纲化处理方式
12. Design of power divider for ads usage record
研究了11种实时聊天软件,我发现都具备这些功能…
Image acquisition and playback of coaxpress high speed camera based on pxie interface
Lumiprobe non fluorescent alkyne EU (5-ethynyluridine)