Home > Web Front-end > JS Tutorial > body text

How to calculate the union of JavaScript arrays?

王林
Release: 2023-09-10 18:49:09
forward
1041 people have browsed it

如何计算 JavaScript 数组的并集?

We can get the union of two arrays by merging the two arrays and removing duplicate elements, and the union gives the unique elements in both arrays.

In this tutorial, we will learn to calculate the union of JavaScript arrays using various methods.

Use set() data structure

The first method is to use the set() data structure. Collection data structures can only contain unique elements. We can add all the elements of both arrays to the set and create a new array from the elements of the set to create the union.

grammar

Users can use the set data structure to calculate the union of JavaScript arrays according to the following syntax.

let union = [...new Set([...array1, ...array2])];
Copy after login

In the above syntax, we use the spread operator to concatenate two arrays and create a new collection from the resulting array. After that, we use the spread operator again to add the elements of the collection to the array.

Example 1

In the example below, array1 and array2 contain some common numbers. First, we connect array1 and array2 using the spread operator. After that, we create a set from the resulting array using the new Set() constructor. The set can only contain unique elements. Therefore, it contains all elements in the union of the two arrays.

After that, we use the spread operator to add all the elements of the collection to the associative array. In the output, the user can observe the union of the two arrays.



   

Using the set() data structure to compute the union of two arrays in JavaScript

Copy after login

Using objects to calculate the union of JavaScript arrays

In this method, we can add array values ​​as object properties. The object always contains unique keys. So we can use the array element as the key of the object and any value for that particular key. After that we can get all object keys to get the union of the arrays.

grammar

Users can use objects to calculate the union of arrays according to the following syntax.

for () {
   union[names1[i]] = 1;
}
for (let key in the union) {
   finalArray.push(key);
}
Copy after login

In the above syntax, first, we push all the array elements as keys of the object. After that we get the keys from the object and add them to the array.

Example 2

In the example below, we have two arrays named names1 and names2. We add the first array element as the object's key. After that, we add the elements of the second array as keys to the object.

Next, we loop through the object, get the object's key and push it to the FinalArray. FinalArray contains the union of the Name1 and Name2 arrays.



   

Using the object to compute the union of two arrays in JavaScript

Copy after login

Use Filter() and Concat() methods

concat() method is used to merge two or more arrays. We can use the filter() method to merge two arrays and filter the unique elements. This way, we can use the concat() and filter() methods to calculate the union of arrays.

grammar

Users can use the filter() and concat() methods to calculate the union of arrays according to the following syntax.

let cities = cities1.concat(cities2);
cities.sort();
let unionCities = cities.filter((value, index) => cities.indexOf(value) === index);
Copy after login

In the above syntax, we first merge the two arrays, sort them, and then use the filter() method to filter the unique elements.

Example 3

In the example below, the city1 and citites2 arrays contain some city names, some of which are common. The cities array contains the array elements of both arrays.

After that, we use the sort() method to sort the city array. Next, we use the filter() method to filter unique values ​​from the cities array. In the filter() method, we pass the callback function as parameter, which checks if the index of the current city is equal to the current index to remove duplicate elements.



   

Using the filter() and concat() methods to compute the union of two arrays in JavaScript

Copy after login

Conclusion

The user learned three different ways to calculate the union of arrays in JavaScript. The first approach requires linear code using a collection data structure. The second method uses objects and the third method uses filter() and concat() methods.

The above is the detailed content of How to calculate the union of JavaScript arrays?. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
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 [email protected]
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!