javascript
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
Was this helpful?
Similar Posts
- Merge multiple arrays and strings into single array using Lodash
- Lodash - merge two arrays and remove duplicates from it
- Combine or merge two or multiple arrays into single array in Javascript
- Assign items of arrays to multiple variable when creating it (Array Destructuring) Javascript(ES6)
- Get all values from an array which does not exist in other array Lodash
- How to Check if Arrays in a Object Are All Empty
- Join two arrays and remove duplicate elements using Javascript