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
Let me summarize a problem that I have encountere...
Preface The keywords of MySQL and Oracle are not ...
This article example shares the specific code of ...
Page domain relationship: The main page a.html bel...
Table of contents Preface Common methods 1. Modif...
There is an interview question that requires: a th...
Docker Compose Docker Compose is a tool for defin...
This article shares the specific code of Vue intr...
Download the MySQL installation package. I downlo...
Table of contents 1. Why do we need unit testing?...
Table of contents Why understand the life cycle W...
Table of contents Background of this series Overv...
Table of contents Solution Analysis slice Resume ...
Preface: As a giant in the IT industry, Microsoft...
This article shares the specific code of JavaScri...