In JavaScript programming, array deduplication is a very common requirement. Usually we will use some algorithms or methods to achieve this operation. But in JavaScript, there are also built-in array deduplication methods for us to use, and they are very convenient to use. This article will introduce the JavaScript array deduplication built-in method and demonstrate the specific usage.
There are three built-in methods for JavaScript array deduplication: Set, indexOf and includes.
1. Set method
Set is a new data structure in ES6. It allows you to store unique values of any type, which ensures that the elements in the set are not repeated. Using the characteristics of Set, you can easily deduplicate arrays.
The specific steps to use the Set method to remove duplicates are as follows:
The sample code is as follows:
const arr = [1, 2, 3, 3, 4, 4, 5];
const newArr = Array.from(new Set (arr));
console.log(newArr); // [1, 2, 3, 4, 5]
In the above code, the Array.from method is used to convert the Set object into an array . The running results show that the duplicate elements in the array arr have been removed.
2. IndexOf method
The indexOf method can query the position of the specified element in the array. If it does not exist, it returns -1. Using this method, we can determine whether an element exists when traversing the array, thereby achieving the purpose of deduplication.
The specific steps to use the indexOf method to remove duplicates are as follows:
The sample code is as follows:
const arr = [1, 2, 3, 3, 4, 4, 5];
const newArr = [];
for (let i = 0; i < arr.length; i ) {
if (newArr.indexOf(arr[i]) === -1) {
newArr.push(arr[i]);
}
}
console.log(newArr); // [1, 2, 3, 4, 5]
In the above code, the purpose of deduplication is achieved by determining whether the element exists in the new array. The running results are the same as those of the Set method.
3. Includes method
The includes method is a new method in ES7. It is used to determine whether the array contains the specified element. For deduplication operations, we can determine whether the new array contains the current element when traversing the array, thereby removing duplicate elements.
The specific steps to use the includes method to remove duplicates are as follows:
The sample code is as follows:
const arr = [1, 2, 3, 3, 4, 4, 5];
const newArr = [];
for (let i = 0; i < arr.length; i ) {
if (!newArr.includes(arr[i])) {
newArr.push(arr[i]);
}
}
console .log(newArr); // [1, 2, 3, 4, 5]
Compared with the indexOf method, the includes method can implement deduplication operations more concisely. The running results are the same as those of the first two methods.
To sum up, there are three commonly used built-in methods for JavaScript array deduplication: Set, indexOf and includes. When using these three methods, you need to choose according to the specific situation. If you are using ES6 or higher JavaScript, it is recommended to use the Set method. If you need compatibility with older versions of JavaScript, you can use the indexOf or includes method. No matter which method is used, array deduplication can be implemented elegantly.
The above is the detailed content of JavaScript array deduplication built-in method. For more information, please follow other related articles on the PHP Chinese website!