A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/javascript/lodash-_-intersection-method/ below:

Lodash _.Intersection() Method - GeeksforGeeks

Lodash _.Intersection() Method

Last Updated : 02 Sep, 2024

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: Return Value:

Example 1: In this example, we are finding the intersection of two arrays.

JavaScript
// Requiring the lodash library
const _ = require("lodash");

// Original array
let array1 = [1, 2, 4, 3, 4, 4]
let array2 = [2, 4, 5, 6]

// Using _.intersection() method
let newArray = _.intersection(
    array1, array2);

// Printing original Array
console.log("original Array1: ", array1)
console.log("original Array2: ", array2)

// Printing the newArray
console.log("new Array: ", newArray)

Output: 

Example 2: In this example, we are finding the intersection of more than two arrays.

JavaScript
// Requiring the lodash library
const _ = require("lodash");

// Original array
let array1 = [1, 2, 4, 3, 4, 4]
let array2 = [2, 4, 5, 6]
let array3 = [2, 3, 5, 6]

// Using _.intersection() method
let newArray = _.intersection(
    array1, array2, array3);

// Printing original Array
console.log("original Array1: ", array1)
console.log("original Array2: ", array2)
console.log("original Array3: ", array3)

// Printing the newArray
console.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 library
const _ = require("lodash");

// Original array
let array1 = [1, 2, 4, 3, 4, 4]
let array2 = []

// Using _.intersection() method
let newArray = _.intersection(array1, array2);

// Printing original Array
console.log("original Array1: ", array1)
console.log("original Array2: ", array2)

// Printing the newArray
console.log("new Array: ", newArray) 

Output: 



RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4