当前位置:网站首页>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
边栏推荐
- Error when connecting MySQL with dbeaver for the first time
- 【GAN】SentiGAN IJCAI’18 Distinguished Paper
- [Gan] Introduction to Gan basics and dcgan
- golang調用sdl2,播放pcm音頻,報錯signal arrived during external code execution。
- The song of cactus - marching into to C live broadcast (2)
- Sharing the strongest summer vacation plan of ape tutoring: the summer vacation plan is the same as learning and playing
- 【GCN-RS】UltraGCN: Ultra Simplification of Graph Convolutional Networks for Recommendation (CIKM‘21)
- Cesium加载3D Tiles模型
- Detailed tutorial on connecting MySQL with tableau
- Cactus Song - online operation (5)
猜你喜欢

Introduction notes to quantum computing (continuously updated)

Advanced usage of setting breakpoints during keil debugging

自然语言处理理论和应用
![[rust daily] January 23, 2022 webapi benchmarking](/img/f7/64f389ff2b8fb481820e577b8531b3.png)
[rust daily] January 23, 2022 webapi benchmarking

如何才能有效缓解焦虑?看看猿辅导怎么说

Golang appelle sdl2, lit l'audio PCM et signale une erreur lors de l'exécution externe du Code.

【实习】跨域问题

关于solidity的delegatecall的坑

Theory and application of naturallanguageprocessing

Correspondence between pytorch and torchvision
随机推荐
Vue failed to connect to MySQL database
[M32] SCM xxx Simple interpretation of map file
代理模式与装饰模式到底哪家强
6. install the SSH connection tool (used to connect the server of our lab)
[out of distribution detection] learning confidence for out of distribution detection in neural networks arXiv '18
Linux link sqlserver, offline installation
C语言——深入理解数组
[rust notes] 01 basic types
Introduction to 51 single chip microcomputer - LED light
[rust notes] 04 expression
Generate string mode
Training penetration range 02 | 3-star VH LLL target | vulnhub range node1
Introduction to 51 single chip microcomputer - 8x8 dot matrix LED
Record of problems caused by WPS document directory update
Test ofnatural clusters via s-dbscan a self tuning version of DBSCAN
OpenGL - Textures
Vector of relevant knowledge of STL Standard Template Library
SQL injection vulnerability (XII) cookie injection
iframe框架,,原生js路由
[write CPU by yourself] implementation of exception related instructions