## Get The Intersection Of Two Arrays In JavaScript

To get the intersection of two arrays in JavaScript –

1. Convert both the arrays to sets to get the unique elements of each array.
2. Convert the first set to an array using the spread operator and call a filter() method passing it a function.
3. On each iteration, check if the element in the array is present in the second set.

Let’s discuss this method in detail below.

Contents

## Get The Intersection Of Two Arrays In JavaScript

Intersection of two arrays means finding all the common elements between two arrays. For example, if array1 = [‘a’,’b’,’c’] and array2 = [‘b’,’c’,’d’], then the intersection of both the arrays will be – b and c.

There are many ways to find the intersection of two arrays in JavaScript. In this article, we will discuss some of the methods to do so.

## Method 1: Using The Spread Operator And filter() method

This is the most efficient and straightforward method to find the intersection of two arrays in JavaScript.

In this method, we first convert both the arrays into sets. A Set is a collection of unique items. So, converting our arrays into sets will filter out all the duplicates from both the arrays. Then, we use the spread operator (…) to convert the first set into an array.

After that, we call the filter() method on this array. The filter() method takes a callback function as its argument.

On each iteration, it checks if the element is present in the second set or not. If it is present, then it is pushed into the resultant array.

Here is the code for this method:

``````const array1 = ['a','b','c'];

const array2 = ['b','c','d'];

// Converting both arrays to sets

const set1 = new Set(array1);

const set2 = new Set(array2);

// Finding the intersection of both sets

// (converting set1 to an array)

const intersection = [...set1].filter(x => set2.has(x));

console.log(intersection);``````

Output: [“b”, “c”]

As you can see from the output, we were able to find the intersection of two arrays in JavaScript using the spread operator and filter() method.

## Method 2: Using The forEach() Method

In this method, we make use of the forEach() method. It executes a callback function on each element of an array.

This method is very similar to the last one, with the only difference being that we use the forEach() method instead of filter(). Here is the code for this method:

``````const array1 = ['a','b','c'];

const array2 = ['b','c','d'];

// Converting both arrays to sets

const set1 = new Set(array1);

const set2 = new Set(array2);

// Creating an empty array to store the intersection

const intersection = [];

// Iterating through set1

set1.forEach((value) => {

// Checking if the value is present in set2 or not

// If it is present, then push into the array

if(set2.has(value)){

intersection.push(value);

}

})

console.log(intersection);``````

Output: [“b”, “c”]

As you can see from the output, we were able to find the intersection of two arrays in JavaScript using the forEach() method.

## Conclusion – Get The Intersection Of Two Arrays In JavaScript

In this article, we discussed some of the methods to get the intersection of two arrays in JavaScript.

We saw how to do so using the spread operator and filter() method as well as the forEach() method.

I hope this article was helpful and that you were able to learn something new from it.

Happy Coding! 🙂