当前位置:网站首页>How js implements array deduplication (7 kinds)
How js implements array deduplication (7 kinds)
2022-08-09 02:02:00 【MomentYY】
目录
JS数组去重的方式
例:将下面数组去除重复元素(以多种数据类型为例)
const arr = [1, 2, 2, 'abc', 'abc', true, true, false, false, undefined, undefined, NaN, NaN]
1.利用Set()+Array.from()
Set对象:是值的集合,You can follow the insertion order迭代它的元素. Set中的元素只会出现一次,即Set中的元素是唯一的.Array.from()方法:对一个类似数组或可迭代对象创建一个新的,浅拷贝的数组实例.
const result = Array.from(new Set(arr))
console.log(result) // [ 1, 2, 'abc', true, false, undefined, NaN ]
注意:以上去方式对NaN和undefined类型去重也是有效的,是因为NaN和undefined都可以被存储在Set中, NaN之间被视为相同的值(尽管在js中:NaN !== NaN).
2.利用两层循环+数组的splice方法
通过两层循环对数组元素进行逐一比较,然后通过splice方法来删除重复的元素.此方法对NaN是无法进行去重的,因为进行比较时
NaN !== NaN.
function removeDuplicate(arr) {
let len = arr.length
for (let i = 0; i < len; i++) {
for (let j = i + 1; j < len; j++) {
if (arr[i] === arr[j]) {
arr.splice(j, 1)
len-- // 减少循环次数提高性能
j-- // 保证j的值自加后不变
}
}
}
return arr
}
const result = removeDuplicate(arr)
console.log(result) // [ 1, 2, 'abc', true, false, undefined, NaN, NaN ]
3.利用数组的indexOf方法
新建一个空数组,遍历需要去重的数组,将数组元素存入新数组中,存放前判断数组中是否已经含有当前元素,没有则存入.此方法也无法对
NaN去重.
indexOf()方法:返回调用它的String对象中第一次出现的指定值的索引,从fromIndex处进行搜索.如果未找到该值,则返回 -1.
function removeDuplicate(arr) {
const newArr = []
arr.forEach(item => {
if (newArr.indexOf(item) === -1) {
newArr.push(item)
}
})
return newArr // 返回一个新数组
}
const result = removeDuplicate(arr)
console.log(result) // [ 1, 2, 'abc', true, false, undefined, NaN, NaN ]
4.利用数组的includes方法
此方法逻辑与indexOf方法去重异曲同工,只是用includes方法来判断是否包含重复元素.
includes()方法:用来判断一个数组是否包含一个指定的值,根据情况,如果包含则返回true,否则返回false.
function removeDuplicate(arr) {
const newArr = []
arr.forEach(item => {
if (!newArr.includes(item)) {
newArr.push(item)
}
})
return newArr
}
const result = removeDuplicate(arr)
console.log(result) // [ 1, 2, 'abc', true, false, undefined, NaN ]
注意:为什么includes能够检测到数组中包含NaN,其涉及到includes底层的实现.如下图为includes实现的部分代码,在进行判断是否包含某元素时会调用sameValueZero方法进行比较,如果为NaN,则会使用isNaN()进行转化.
具体实现可参考:https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array/includes

简单测试includes()对NaN的判断:
const testArr = [1, 'a', NaN]
console.log(testArr.includes(NaN)) // true
5.利用数组的filter()+indexOf()
filter方法会对满足条件的元素存放到一个新数组中,结合indexOf方法进行判断.
filter()方法:会创建一个新数组,其包含通过所提供函数实现的测试的所有元素.
function removeDuplicate(arr) {
return arr.filter((item, index) => {
return arr.indexOf(item) === index
})
}
const result = removeDuplicate(arr)
console.log(result) // [ 1, 2, 'abc', true, false, undefined ]
注意:这里的输出结果中不包含NaN,是因为indexOf()无法对NaN进行判断,即arr.indexOf(item) === index返回结果为false.测试如下:
const testArr = [1, 'a', NaN]
console.log(testArr.indexOf(NaN)) // -1
6.利用Map()
Map对象是JavaScript提供的一种数据结构,结构为键值对形式,将数组元素作为map的键存入,然后结合
has()和set()方法判断键是否重复.
Map对象:用于保存键值对,并且能够记住键的原始插入顺序.任何值(对象或者原始值)都可以作为一个键或一个值.
function removeDuplicate(arr) {
const map = new Map()
const newArr = []
arr.forEach(item => {
if (!map.has(item)) {
// has()用于判断map是否包为item的属性值
map.set(item, true) // 使用set()将item设置到map中,并设置其属性值为true
newArr.push(item)
}
})
return newArr
}
const result = removeDuplicate(arr)
console.log(result) // [ 1, 2, 'abc', true, false, undefined, NaN ]
注意:使用Map()也可对NaN去重,原因是Map进行判断时认为NaN是与NaN相等的,剩下所有其它的值是根据 === 运算符的结果判断是否相等.
7.利用对象
其实现思想和
Map()是差不多的,主要是利用了对象的属性名不可重复这一特性.
function removeDuplicate(arr) {
const newArr = []
const obj = {
}
arr.forEach(item => {
if (!obj[item]) {
newArr.push(item)
obj[item] = true
}
})
return newArr
}
const result = removeDuplicate(arr)
console.log(result) // [ 1, 2, 'abc', true, false, undefined, NaN ]
边栏推荐
猜你喜欢

eladmin容器部署超详细过程

Group DETR:分组一对多匹配是加速DETR收敛的关键

JDBC技术(三)——使用Druid数据库连接池测试

企业里Foxmail邮箱问题解决方法汇总
![class path resource [bean.xml] cannot be opened because it does not 错误解决方案](/img/e2/6430a511998944a3357984b554b00c.png)
class path resource [bean.xml] cannot be opened because it does not 错误解决方案

【物理应用】基于El-centro地震波作用下隔震与非隔震支座下的顶层位移、速度、加速度的对比情况附matlab代码

PostMan导入证书 添加证书

How to install ngrok in Synology system (Synology 6.X version)

MT4/MQ4L入门到精通EA教程第二课-MQL语言常用函数(二)-账户信息常用功能函数

如何在群晖系统中安装cpolar(群晖6.X版)
随机推荐
力扣刷题记录10.1-----19. 删除链表的倒数第 N 个结点
全文翻译:EDPB关于VVA(虚拟语音助理)中处理个人数据的指南02/2021
mysql连接超过八小时报错
Codeforces Round #809 (Div. 2)A~D1
2022/8/8 比赛思维+状压dp
《LC刷题总结》——贪心
程序员的日常生活 | 每日趣闻
配置文件的读取-TOML
PostMan import certificate add certificate
Cmake 报错 Could not find a package configuration file provided by “OpenCV“
[Signal denoising] Based on Sage-Husa adaptive Kalman filter to realize the suppression of ocean wave magnetic field noise and the generation of ocean wave magnetic field noise with matlab code
Go-12-Structure
yii2的安装之路
如何在EasyDSS中使用ffmpeg实现点播视频的拼接与合成?
【图像去噪】基于边缘增强扩散 (cEED) 和 Coherence Enhancing Diffusion (cCED) 滤波器实现图像去噪附matlab代码
电磁辐射安全标准及检测方法
【物理应用】基于El-centro地震波作用下隔震与非隔震支座下的顶层位移、速度、加速度的对比情况附matlab代码
How to install ngrok in Synology system (Synology 6.X version)
gstreamer 记录
智能视频监控设计摄像头部分