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
virtualenv is a tool for creating isolated Python...
Step 1: Configure environment variables (my decom...
<br />Related article: Analysis of Facebook&...
This article example shares the specific code of ...
1. Use CSS, jQuery, and Canvas to create animatio...
For example, there is an input box <el-input r...
Preface In MySQL, we can use the EXPLAIN command ...
describe This article introduces a method to impl...
Table of contents What is a plugin Writing plugin...
This article example shares the specific code of ...
Table of contents 1. Forgot the root password and...
1. Implement call step: Set the function as a pro...
Bugs As shown in the figure, I started to copy th...
answer from stackflow: Simply <br> is suffic...
Preface The mv command is the abbreviation of mov...