1 Test Cases// Test case const a = {}; const b = { c: 1 }; const array = [ 1, 1, "1", "1", {}, {}, { c: 1 }, { c: 1}, a, a, b, b, [], [], [1], [1], undefined, undefined, null, null, NaN, NaN, ]; 2 JS array deduplication 4 types2.1 Element ComparisonThis type removes duplicates by comparing array elements. 2.1.1 Double-layer for loop comparison (commonly used in es5) Use a double for loop to compare array elements one by one, and use // Double for loop function uniq1(arr) { for (let i = 0; i < arr.length; i++) { for (let j = i + 1; j < arr.length; j++) { if (arr[i] === arr[j]) { arr.splice(j, 1) j-- } } } return arr } // Deduplication result// [1,'1',{},{},{c:1},{c:1},{},{c:1},[],[],[1],[1],undefined,null,NaN,NaN] By comparing the results before and after deduplication, the duplicate 2.1.2 Sorting adjacent comparison Use the function uni2(arr) { arr.sort(); for (let i = 0; i < arr.length - 1; i++) { arr[i] === arr[i + 1] && arr.splice(i + 1, 1) && i--; } return arr; } You can also create a new array and put non-repeating elements into the new array function uniq3(arr) { arr = arr.sort() const newArr = [arr[0]] for (let i = 1; i < arr.length; i++) { if (arr[i] !== arr[i - 1]) { newArr.push(arr[i]) } } return newArr } // Deduplication result// [[],[],1,'1',[1],[1],NaN,NaN,{},{},{c:1},{c:1},{},{c:1},null,undefined] Duplicate 2.2 Find element position typeThis type removes duplicates by finding the first occurrence of an element. 2.2.1 indexOf Use function uniq4(arr) { let res = [] for (let i = 0; i < arr.length; i++) { if (arr.indexOf(arr[i]) === i) { res.push(arr[i]) } } return res } // Deduplication result// [1,'1',{},{},{c:1},{c:1},{},{c:1},[],[],[1],[1],undefined,null] Similarly, because 2.2.2 findIndex Use function uniq5(arr) { let res = [] for (let i = 0; i < arr.length; i++) { if (arr.findIndex(item => item === arr[i]) === i) { res.push(arr[i]) } } return res } // Deduplication result// [1,'1',{},{},{c:1},{c:1},{},{c:1},[],[],[1],[1],undefined,null] Similarly, because 2.3 Element Existence TypeThis type removes duplicates by determining whether the current element exists in the new array. 2.3.1 includes The function uniq6(arr) { let res = [] for (let i = 0; i < arr.length; i++) { if (!res.includes(arr[i])) { res.push(arr[i]) } } return res } // Deduplication result// [1,'1',{},{},{c:1},{c:1},{},{c:1},[],[],[1],[1],undefined,null,NaN]
2.3.2 some The function uniq7(arr) { let res = [] for (let i = 0; i < arr.length; i++) { if (!res.some(item => item === arr[i])) { res.push(arr[i]) } } return res } // Deduplication result// [1,'1',{},{},{c:1},{c:1},{},{c:1},[],[],[1],[1],undefined,null,NaN,NaN] Again, 2.4 Relying on data structure characteristics This type uses the non-repeatable characteristics of the data structures 2.4.1 Map The Map structure provided by function uniq8(arr) { const map = new Map() for (let i = 0; i < arr.length; i++) { !map.has(arr[i]) && map.set(arr[i], true) } return [...map.keys()] } // Deduplication result// [1,'1',{},{},{c:1},{c:1},{},{c:1},[],[],[1],[1],undefined,null,NaN]
2.4.2 Set (ES6 most commonly used) The values of the members of the function uniq9(arr) { return [...new Set(arr)] } // Deduplication result// [1,'1',{},{},{c:1},{c:1},{},{c:1},[],[],[1],[1],undefined,null,NaN] 3 Supplement The methods mentioned above can be modified using different For example, in the In short, there are many methods, but they all remain the same. Some deduplication methods are not valid for In practical applications, the most common method is to use Set, or you can use the third-party library This is the end of this article about the details of JS array deduplication. For more relevant JS array deduplication content, please search 123WORDPRESS.COM’s previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: Java uses Apache.POI to export HSSFWorkbook to Excel
>>: Detailed graphic explanation of mysql query control statements
Linux remote deployment of MySQL database, for yo...
Table of contents history pushState() Method push...
Table of contents Install mysql Configuring envir...
Solution Add position:relative to the parent elem...
This article shares the specific code of Vue to i...
1. Replace your .js library file address with the...
Table of contents 1. Panorama II. Background 1. R...
<br />Related article: Analysis of Facebook&...
Table of contents Object.prototype.valueOf() Obje...
Because frameset and body are on the same level, y...
This article shares the specific code of JavaScri...
When server B (172.17.166.11) is powered on or re...
I have searched various major websites and tested...
Generally, after there is a menu on the left, the...
The <script> tag In HTML5, script has the f...