当前位置:网站首页>Several methods of array de duplication in JS
Several methods of array de duplication in JS
2022-06-22 07:00:00 【webchang】
Code demonstration :
let arr = [1,1,'true','true',15,15,false,false, undefined,undefined, null,null, NaN, NaN,'NaN', 'a', 'a'];
// Method 1, It can be done to NaN Deduplication , because Set When we add value, we think that NaN Equal to itself
function f1(arr) {
// return Array.from(new Set(arr));
return [...new Set(arr)];
}
console.log(' Method 1:',f1(arr));
// Method 2, You can't be right NaN Deduplication (indexOf Method does not recognize the NaN member )
function f2(arr) {
let result = [];
for (let i = 0; i < arr.length; i++) {
if (result.indexOf(arr[i]) === -1) {
result.push(arr[i]);
}
}
return result;
}
console.log(' Method 2:',f2(arr));
// Method 3, Use ES6 Of findIndex Method , Can combine Object.is() Method pair NaN Deduplication
// Object.is() The method is that NaN Equal to itself
function f3(arr) {
let result = [];
for (let i = 0; i < arr.length; i++) {
if (result.findIndex(value => Object.is(arr[i],value)) === -1) {
result.push(arr[i]);
}
}
return result;
}
console.log(' Method 3:',f3(arr));
// Method 4: It can be done to NaN Deduplication
// Array of includes() The method is that NaN Equal to itself
function f4(arr) {
let result = [];
for (let i = 0; i < arr.length; i++) {
if (!result.includes(arr[i])) {
result.push(arr[i]);
}
}
return result;
}
console.log(' Method 4:',f4(arr));
// Method 5: This method will NaN Jump over , The final result will not include NaN
// indexOf The strict equality operator is used internally (===) Judge , This will lead to the right NaN Miscarriage of Justice .
function f5(arr) {
return arr.filter((value,index) => {
return arr.indexOf(value) === index;
});
}
console.log(' Method 5:',f5(arr));
The above methods are similar , The following pair involves NaN Make a summary of the contents of :
NaN == NaN // false
NaN === NaN // false
// indexOf Method does not recognize the NaN member
[NaN].indexOf(NaN) // -1
// towards Set When a value is added to a data structure, it is considered that NaN Equal to itself
let set = new Set();
set.add(NaN);
set.add(NaN);
console.log(set); // Set {NaN}
// Object.is() The method is that NaN be equal to NaN
Object.is(NaN, NaN) // true
+0 === -0 //true
Object.is(+0, -0) // false
// ES2016 New array instance method in :includes() The method is that NaN Equal to itself
[1, 2, NaN].includes(NaN) // true
Front end learning exchange QQ Group , The atmosphere of learning and discussion in the group is very good , There are a lot of big people , Looking forward to your joining :862748629 Click to add
边栏推荐
- sessionStorage 和 localStorage 的使用
- 6. install the SSH connection tool (used to connect the server of our lab)
- 编程题:移除数组中的元素(JS实现)
- How can we effectively alleviate anxiety? See what ape tutor says
- leetcode:面试题 08.12. 八皇后【dfs + backtrack】
- Py's optbinning: introduction, installation and detailed introduction of optbinning
- [fundamentals of machine learning 01] blending, bagging and AdaBoost
- 【GAN】SentiGAN IJCAI’18 Distinguished Paper
- Sharing the strongest summer vacation plan of ape tutoring: the summer vacation plan is the same as learning and playing
- Cesium loading 3D tiles model
猜你喜欢
![[out of distribution detection] learning confidence for out of distribution detection in neural networks arXiv '18](/img/07/d5479dde181c355d95c73e2f58c0da.jpg)
[out of distribution detection] learning confidence for out of distribution detection in neural networks arXiv '18

Introduction to 51 Single Chip Microcomputer -- the use of Keil uvision4

buuctf部分题目wp
![[fundamentals of machine learning 04] matrix factorization](/img/f5/373bfe68f1a3422e907056c20a0db3.jpg)
[fundamentals of machine learning 04] matrix factorization

OpenGL - Draw Triangle

首次用DBeaver连接mysql报错
![[internship] cross domain problems](/img/0c/48b3138eb153cf4402017924e564a0.jpg)
[internship] cross domain problems
![Leetcode: interview question 08.12 Eight queens [DFS + backtrack]](/img/d0/e28af1a457f433b35972fd807b8ad7.png)
Leetcode: interview question 08.12 Eight queens [DFS + backtrack]

Introduction to 51 Single Chip Microcomputer -- the use of Proteus 8 professional

OpenGL - Textures
随机推荐
C language - deep understanding of arrays
QT connect to Alibaba cloud using mqtt protocol
Anaconda introduction, installation and use nanny level tutorial
What exactly is the open source office of a large factory like?
Neuron+ekuiper realizes data collection, cleaning and anti control of industrial Internet of things
深度解析Optimism被盗2000万个OP事件(含代码)
The journey of an operator in the framework of deep learning
Five common SQL interview questions
The song of cactus - marching into to C live broadcast (1)
六月集训(第22天) —— 有序集合
Reprint the Alibaba open source project egg JS technical documents cause "copyright disputes". How to use the loose MIT license?
[rust notes] 01 basic types
生成字符串方式
Test ofnatural clusters via s-dbscan a self tuning version of DBSCAN
Event preview edgex developer summit @ Nanjing station is coming!
buuctf部分题目wp
Successfully solved raise keyerror (F "none of [{key}] are in the [{axis\u name}]") keyerror: "none of [index (['age.in.y
Buuctf part Title WP
[anomaly detection] malware detection: mamadroid (dnss 2017)
Detailed tutorial on connecting MySQL with tableau