Lodash _.intersection() method is used to take the intersection of one or more arrays. It is the same as the intersection in set theory.
Syntax:
_.intersection([arrays]);
Parameters:
- It takes an array as a parameter.
Return Value:
It returns the array after the intersection of arrays.
Example 1: In this example, we are finding the intersection of two arrays.
Javascript
// Requiring the lodash libraryconst _ = require("lodash");Â
// Original arraylet array1 = [1, 2, 4, 3, 4, 4]let array2 = [2, 4, 5, 6]Â
// Using _.intersection() methodlet newArray = _.intersection(Â Â Â Â array1, array2);Â
// Printing original Arrayconsole.log("original Array1: ", array1)console.log("original Array2: ", array2)Â
// Printing the newArrayconsole.log("new Array: ", newArray) |
Output:Â
Example 2: In this example, we are finding the intersection of more than two arrays.
Javascript
// Requiring the lodash libraryconst _ = require("lodash");Â
// Original arraylet array1 = [1, 2, 4, 3, 4, 4]let array2 = [2, 4, 5, 6]let array3 = [2, 3, 5, 6]Â
// Using _.intersection() methodlet newArray = _.intersection(Â Â Â Â array1, array2, array3);Â
// Printing original Arrayconsole.log("original Array1: ", array1)console.log("original Array2: ", array2)console.log("original Array3: ", array3)Â
// Printing the newArrayconsole.log("new Array: ", newArray) |
Output:Â
Example 3:In this example, we are finding the intersection of two arrays in which one is empty that is why we did not get any new intersected array as they do not have common elements.
Javascript
// Requiring the lodash libraryconst _ = require("lodash");Â
// Original arraylet array1 = [1, 2, 4, 3, 4, 4]let array2 = []Â
// Using _.intersection() methodlet newArray = _.intersection(array1, array2);Â
// Printing original Arrayconsole.log("original Array1: ", array1)console.log("original Array2: ", array2)Â
// Printing the newArrayconsole.log("new Array: ", newArray) |
Output:Â

