There are several other ways to process arrays~
Method 1: set: It is not a data type, but a data structure; members are uniquelet arr = [12,1,12,3,1,88,66,9,66]; function unique(ary) { let s = new Set(ary); // Array.from: Convert the set data structure into a real array; return Array.from(s) } unique(arr); Method 2: Object attribute names cannot be repeatedlet arr = [12,1,12,3,1,88,66,9,66]; function unique(ary) { let obj = {}; for(let i=0;i<ary.length;i++){ let cur = ary[i]; if (obj[cur]) { //ary.splice(i,1);// causes the array to collapse ary[i]=ary[ary.length-1]; ary.length--; // delete the last item i--; continue; } obj[cur]=cur; // Add a key-value pair to obj; the attribute name and attribute value are the same} } unique(arr); Method 3: indexOflet arr = [12,1,12,3,1,88,66,9,66]; function unique(ary) { let newAry = []; for(let i=0;i<ary.length;i++){ let cur = ary[i]; if (newAry.indexOf(cur)===-1){ newAry.push(cur); } } return newAry; } unique(arr) Method 4: sortlet arr = [12,1,12,3,1,88,66,9,66]; function unique(ary) { let a = ary.sort(function (a,b) { return ab; }); for(let i=0;i<a.length;i++){ if(a[i]===a[i+1]){ a.splice(i+1,1); i--; } } return a; } unique(arr) Method 5: includes: includes; if the array contains that item, returns true; if it does not contain it, returns false;let arr = [12,1,12,3,1,88,66,9,66]; function unique(ary) { let newAry = []; let len = ary.length; for(let i=0;i<len;i++){ let cur = ary[i]; if (!newAry.includes(cur)){ newAry.push(cur); } } return newAry; } console.log(unique(arr)); Method 6: hasOwnProperty: Checks whether the property name is a private property of the object; returns a Boolean value;let arr = [12,1,12,3,1,88,66,9,66]; function unique(ary) { let obj = {}; return ary.filter(function (item,index,a) { // item: each member of the array // index: the index corresponding to the member // a: the entire array // hasOwnProperty is used to check whether the property has appeared; return obj.hasOwnProperty(typeof item+item)?false:obj[typeof item+item]=true; if (obj.hasOwnProperty(typeof item+item)){ return false }else{ obj[typeof item+item]=true; return true; } }) } console.log(unique(arr)) Method 7: filter+indexOflet arr = [12,1,12,3,1,88,66,9,66]; function unique(ary) { return ary.filter(function (item,index,a) { return ary.indexOf(item)===index; }) } console.log(unique(arr)); Method 8: splicelet arr = [12,1,12,3,1,88,66,9,66]; function unique(ary) { for(let i=0;i<ary.length;i++){ for(j=i+1;j<ary.length;j++){ if(ary[i]===ary[j]){ ary.splice(j,1); j--; } } } return ary; } unique(arr); Method 9: Recursionlet arr = [12,1,12,3,1,88,66,9,66]; function unique(ary) { let len = ary.length; ary = ary.sort(function (a,b) { return ab; }); function loop(index) { if(index>=1){ if(ary[index]===ary[index-1]){ ary.splice(index,1); } loop(index-1) } } loop(len-1); return ary; } console.log(unique(arr)); Method 10: Map: Utilizes the value storage feature of Map data structure;let arr = [12,1,12,3,1,88,66,9,66]; function unique(ary) { let newAry = []; let map = new Map(); for(let i=0;i<ary.length;i++){ if (!map.has(ary[i])){ map.set(ary[i],true); newAry.push(ary[i]); } } } unique(arr); Method 11: reducelet arr = [12,1,12,3,1,88,66,9,66]; function unique(ary) { // reduce: The first one is a function, and the second parameter will be passed to prev of the first callback; return ary.reduce((prev,next)=>{ //The return value of this function is prev for the next execution; return prev.includes(next)?prev:[...prev,next]; },[]) } console.log(unique(arr)); Method 12: Similar to method 1, using the rest operator...let arr = [12,1,12,3,1,88,66,9,66]; let a = [...new Set(arr)]; console.log(a); This is the end of this article about JS array solutions. For more relevant JS array deduplication content, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: A brief analysis of MySQL parallel replication
>>: UDP connection object principle analysis and usage examples
First query table structure (sys_users): SELECT *...
<br />With the increase of bandwidth, there ...
Introduction To put it simply, tcpdump is a packe...
The previous article introduced two methods to ch...
Service.xml The Server.xml configuration file is ...
Table of contents Code Optimization Using key in ...
This article describes the steps to install the p...
This article is translated from the blog Usability...
Table of contents introduce Prepare Download syst...
01 The concept of parallel replication In the mas...
Official website: http://code.google.com/p/zen-cod...
Problem background: There is a requirement to tak...
This article mainly focuses on the installation a...
Table of contents 1. Overview of the page 2. Infi...
Preparation First, you need to download nodejs, w...