Search code snippets, questions, articles...

Lodash - Get common values from multiple arrays

How can we get intersection of two or multiple arrays. Intersection of arrays is common values array that exists in all input arrays. You can get it using lodash function _.intersection().
const arr1 = [10, 20, 30];
const arr2 = [5, 20, 40, 30];

const result1 = _.intersection(arr1, arr2);
// => [20,30]

const arr3 = [20];
const result2 = _.intersection(arr1, arr2, arr3);
// => [20]

In the first example of code snippet, we have defined two arrays arr1 and arr2 and we are getting common values array or intersection array of them. While in the second example we are getting the intersection of three arrays.

_.intersection Lodash

_.intersection(Array1, Array2, ...)

You can pass as many arrays as you want to get the common values between them.

Example Demo

Search Index Data (The code snippet can also be found with below search text)

Get intersection of multiple array lodash
_.intersection Lodash
Was this helpful?
0 Comments
Programming Feeds
Learn something new everyday on Devsheet