当前位置:网站首页>JS array de duplication
JS array de duplication
2022-07-16 06:10:00 【Campus ascetic】
1、 Traverse + Array.prototype.indexOf duplicate removal .
var arr = [1, '1', 1, 'str', NaN, NaN, null, null, undefined, undefined];
function deleteDuplicate(a) {
if( Object.prototype.toString.call( a ) !== '[object Array]' || !a.length ) {
return a;// The array 、 Empty array directly returns
}
var tmp = [ a[0] ];// Store the first element of the original array
for(var i = 1; i < a.length; i++) {
// Start traversing from the second
if( tmp.indexOf( a[i] ) === -1 ) {
tmp.push( a[i] );
}
}
return tmp;
}
deleteDuplicate( arr );
//[1, "1", "str", NaN, NaN, null, undefined]
// You can't get rid of it NaN, have access to ES2016 The introduced array method
//[1, 2, NaN].includes( NaN ) === true2、 For pure numbers , Pure character array , You can use js Object properties , Object key is unique and character .
var arr = [1, 2, 4, 2, 4, 3, 1];
function deleteDuplicate(a) {
if( Object.prototype.toString.call( a ) !== '[object Array]' || !a.length ) {
return a;
}
var obj = {}, tmp = [];
for(var i = 0; i < a.length; i++) {
obj[ a[i] ] = a[i];
}
for( var key in obj ) {
tmp.push( obj[key] );
}
return tmp;
}
deleteDuplicate( arr );
//[1, 2, 3, 4]
// Because the keys of traditional objects are characters ,obj[1] and obj['1'] It's the same effect , And the original order cannot be guaranteed 3、ES6 Provides Map data structure . It's like an object , It's also a set of key value pairs , however “ key ” Is not limited to strings , Various types of values ( Including objects ) Can be used as a key . in other words ,Object Structure provides “ character string — value ” Corresponding ,Map Structure provides “ value — value ” Corresponding , Is a more perfect Hash Structure implementation . If you need “ Key value pair ” Data structure of ,Map Than Object More appropriate , also Map The traversal order of is the insertion order .
let arr = [NaN, 1, [1], [1], 1, '1', 4, 1, 2, 4, 5, 5, NaN, NaN, null, null, undefined, undefined];
function deleteDuplicate(a) {
if( !Array.isArray( a ) || !a.length ) {
return a;
}
let map = new Map();
a.forEach( value => map.set(value, value) );
return Array.from( map.values() );// return [ ...map.values() ];
}
deleteDuplicate( arr );
//[NaN, 1, Array(1), Array(1), "1", 4, 2, 5, null, undefined]
//Map The traversal order of is the insertion order 4、ES6 Provides a new data structure Set. It's like an array , But the values of the members are unique , There are no duplicate values .
let arr = [NaN, 1, [1], [1], 1, '1', 4, 1, 2, 4, 5, 5, NaN, NaN, null, null, undefined, undefined];
function deleteDuplicate(a) {
if( !Array.isArray( a ) || !a.length ) {
return a;
}
let set = new Set( a );
return Array.from( set );// return [ ...set ];
}
deleteDuplicate( arr );
//[NaN, 1, Array(1), Array(1), "1", 4, 2, 5, null, undefined]边栏推荐
- Help me up, I can continue to edit!
- 什么是慢查询?如何优化?
- NFT 交易市场主要使用 ETH 本位进行交易的局面是如何形成的?
- How did the situation that NFT trading market mainly uses eth standard for trading come into being?
- Reflection get member methods and member variables
- 計算LocalDate之間的天數差,方便快捷
- MySQL-DQL-条件查询/聚合函数/分组查询/排序查询/分页查询
- Nftscan Developer Platform launches Multi Chain NFT data pro API service
- 将BufferedImage转为byte[]数组,亲测可用
- 蚂蚁集团开源大规模视频侵权定位数据集
猜你喜欢

Notes 2 of network communication security

MSF Foundation

39.js-- scope

How to solve the "Impossible Triangle" problem of data flow?

chrome浏览器91版本SameSite by default cookies被移除后的解决方案,Chrome中跨域POST请求无法携带Cookie的解决方案

NFTScan 开发者平台推出多链 NFT 数据 Pro API 服务

你真的懂JS事件循环吗

SNMP started

Notes 1 of network communication security

防抖与节流:实践与勘误
随机推荐
Es6--string (string)
PLSQL problem solving ora-12154: tns: unable to resolve the specified connection identifier
JS scope chain
Analyse, capture et simulation du Protocole ARP par sniffer pro
37.js -- member operation of object and operation of prototype object (mainly related program examples)
消息转发机制--拯救你的程序崩溃
Notes on network communication security -- OSPF theory and experiment
Infrastructure nftscan officially released Solana network NFT browser
《sql必知必会》系列1基础检索相关
编译原理-语法分析器设计
Help me up, I can continue to edit!
_button.enable=NO不起作用
欧拉Talk | 开发者社区体验坦白局7月14日19:30约起
【论文翻译】Issues and Challenges of Aspect-based Sentiment Analysis: A Comprehensive Survey
Graphic and image programming practice course report
MySQL constraint
Tkmapper uses weekend splicing conditions to query conditions
rollup 打包实践
JS time object
chrome浏览器91版本SameSite by default cookies被移除后的解决方案,Chrome中跨域POST请求无法携带Cookie的解决方案