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
The default database of CentOS7 is mariadb, but m...
By default, the MyISAM table will generate three ...
This article uses an example to describe the erro...
Table of contents Join syntax: 1. InnerJOIN: (Inn...
This article records the specific method of insta...
The installation of MySQL 5.7 on Ubuntu 1804 is i...
There are obvious differences between volume moun...
1. Create a new configuration file docker_nginx.c...
This article shares the specific code for WeChat ...
The 2008.5.12 Wenchuan earthquake in Sichuan took...
I have been learning about responsive design rece...
Similar structures: Copy code The code is as foll...
1. Command Introduction The ln command is used to...
<br /> Focusing on the three aspects of text...
We all know that after the MySQL database is inst...