JavaScript array deduplication built-in method

PHPz
Release: 2023-05-09 22:02:07
Original
495 people have browsed it

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:

  1. Define an empty Set object;
  2. Traverse the array and store the array elements as the value of the Set object Enter;
  3. Convert the Set object into an array.

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:

  1. Define an empty array;
  2. Traverse the original array arr, if the current array does not exist in the new array newArr element, the current element is added to newArr.

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]);
Copy after login
Copy after login

}
}
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:

  1. Define an empty array;
  2. Traverse the original array arr, if the new array newArr does not contain the current element, the current element is added to newArr.

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]);
Copy after login
Copy after login

}
}
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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!