# Get the intersection of two arrays in JavaScript

We will learn how to get the intersection of two arrays in JavaScript using two different methods that are using filter() and lodash intersection(). Each method is relatively straightforward, and you can choose whatever you want.

## How to get the intersection of two arrays in JavaScript

### Using filter()

The filter() method returns an array of elements that satisfy a checking function. Here is its syntax and usage:

Syntax:

filter(function, this)

Parameters:

• function: The prototype of function or function name
• this: a value to use as this when executing the function

If you have two arrays that contain some of the values and you want to get the intersection, you can use one of these to apply the filter() method, and the checking function will check if the other array contains these elements or not:

let arr1 = ["learn", "share", "it"];
let arr2 = ["it", "learn"];

// Get the intersection of two arrays in JavaScript
let res = arr1.filter(Set.prototype.has, new Set(arr2));
console.log(res);

Output:

['learn', 'it']

The above example uses the prototype of set type to check if a value already exists in the array. It can be seen from the example that we have got the intersection between arr1 and arr2. However, if the first array contains duplicate values itself, for example:

let arr1 = ["learn", "learn", "share", "it"];
let arr2 = ["it", "learn"];

// Get the intersection of two arrays in JavaScript
let res = arr1.filter(Set.prototype.has, new Set(arr2));
console.log(res);

Output:

['learn', 'learn', 'it']

Is the output an intersection of two arrays? It depends on your expectations. The intersection is a term related to a set (containing different values), and people may often need clarification on these types. However, if you don’t want this result to happen, you can convert the array into a set and convert it back to the array if you want:

let arr1 = ["learn", "learn", "share", "it"];
let arr2 = ["it", "learn"];

// Get the intersection of two arrays in JavaScript
let arr = arr1.filter(Set.prototype.has, new Set(arr2));
console.log(arr);

// Convert an array to a set to remove duplicates
let mySet = new Set(arr);
console.log(mySet);

// Convert the set to the array if you want
let res = [...mySet];
console.log(res);

Output:

 ['learn', 'learn', 'it']
Set(2) {'learn', 'it'}
['learn', 'it']

### Using _.intersection()

This method is from the lodash library; you have to import it to use. You can read this tutorial to see how to install it. Below is its syntax:

Syntax:

_.intersection(array1, array2)

Parameters:

• array1: The first array to get the intersection
• array2: The second array to get the intersection

As you can see, it is very easy to use this method, for example:

console.log(_.intersection(["0", "1", "2"], ["3", "2", "1"]));

Output:

['1','2']

Another test case:

console.log(_.intersection(["learn", "learn", "share", "it"], ["it", "learn"]));

Output:

['learn', 'it']

As can be seen, this method produces the expected output without duplicate values in the arrays.

## Summary

We have learned how to get the intersection of two arrays in JavaScript with two different methods. The second solution of lodash intersection() will consume less time and is more efficient. We hope you will be successful using our tutorial. Good lucks to you.

Maybe you are interested: