Example: Remove duplicate elements from the following array (taking multiple data types as examples) const arr = [1, 2, 2, 'abc', 'abc', true, true, false, false, undefined, undefined, NaN, NaN] 1. Using Set()+Array.from()
const result = Array.from(new Set(arr)) console.log(result) // [ 1, 2, 'abc', true, false, undefined, NaN ]
2. Using two-layer loop + array splice method The array elements are compared one by one through two loops, and then function removeDuplicate(arr) { let len = arr.length for (let i = 0; i < len; i++) { for (let j = i + 1; j < len; j++) { if (arr[i] === arr[j]) { arr.splice(j, 1) len-- // Reduce the number of loops to improve performance j-- // Ensure that the value of j remains unchanged after addition} } } return arr } const result = removeDuplicate(arr) console.log(result) // [ 1, 2, 'abc', true, false, undefined, NaN, NaN ] 3. Using the indexOf method of arrayCreate a new empty array, traverse the array that needs to be deduplicated, and store the array elements in the new array. Before storing, determine whether the array already contains the current element. If not, store it. This method also cannot remove duplicates of NaN. indexOf() method: Returns the index of the first occurrence of a specified value in function removeDuplicate(arr) { const newArr = [] arr.forEach(item => { if (newArr.indexOf(item) === -1) { newArr.push(item) } }) return newArr // Return a new array } const result = removeDuplicate(arr) console.log(result) // [ 1, 2, 'abc', true, false, undefined, NaN, NaN ] 4. Use the includes method of array The logic of this method is similar to that of includes() method: used to determine whether an array contains a specified value. Depending on the situation, it returns true if it contains a value, otherwise it returns false. function removeDuplicate(arr) { const newArr = [] arr.forEach(item => { if (!newArr.includes(item)) { newArr.push(item) } }) return newArr } const result = removeDuplicate(arr) console.log(result) // [ 1, 2, 'abc', true, false, undefined, NaN ]
Simple test includes()'s judgment on NaN: const testArr = [1, 'a', NaN] console.log(testArr.includes(NaN)) // true 5. Using filter()+indexOf() of array filter() method: creates a new array containing all elements that pass the test implemented by the provided function. function removeDuplicate(arr) { return arr.filter((item, index) => { return arr.indexOf(item) === index }) } const result = removeDuplicate(arr) console.log(result) // [ 1, 2, 'abc', true, false, undefined ]
const testArr = [1, 'a', NaN] console.log(testArr.indexOf(NaN)) // -1 6. Using Map() The Map object is a data structure provided by Map object: used to store key-value pairs and remember the original insertion order of the keys. Any value (object or primitive) can be used as a key or a value. function removeDuplicate(arr) { const map = new Map() const newArr = [] arr.forEach(item => { if (!map.has(item)) { // has() is used to determine whether the map contains the attribute value of item map.set(item, true) // Use set() to set item to the map and set its attribute value to true newArr.push(item) } }) return newArr } const result = removeDuplicate(arr) console.log(result) // [ 1, 2, 'abc', true, false, undefined, NaN ]
7. Target of use Its implementation idea is similar to function removeDuplicate(arr) { const newArr = [] const obj = {} arr.forEach(item => { if (!obj[item]) { newArr.push(item) obj[item] = true } }) return newArr } const result = removeDuplicate(arr) console.log(result) // [ 1, 2, 'abc', true, false, undefined, NaN ] This concludes this article about seven ways to implement array deduplication in JS. For more information on JS array deduplication, please search previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: favico.ico---Website ico icon setting steps
>>: Typora code block color matching and title serial number implementation code
Table of contents 1. Create objects by literal va...
The first time I installed MySQL on my virtual ma...
Let’s take a look at the panoramic view effect: D...
First of all, let me talk to you about my daily l...
1. What is responsive design? Responsive design i...
In a web page, the <input type="file"...
Sometimes we want to implement such a function: c...
Preface As a DBA, you will often encounter some M...
The following questions are all based on the Inno...
The first article on data backup and restoration ...
1. MHA By monitoring the master node, automatic ...
A master once said that you should know the datab...
Pure front-end implementation:切片上傳斷點續傳.斷點續傳needs ...
vue scaffolding -> vue.cli Quickly create a la...
There is such a requirement: an import button, cl...