how to use the intersectionby function from the lodash library in typescript

To use the intersectionBy function from the Lodash library in TypeScript, you first need to install the Lodash library:

index.ts
npm install lodash
19 chars
2 lines

Once you have installed the Lodash library, you can import the intersectionBy function and use it as follows:

index.ts
import * as _ from 'lodash';

const arr1 = [{ 'x': 1 }, { 'x': 2 }, { 'x': 3 }];
const arr2 = [{ 'x': 2 }, { 'x': 3 }, { 'x': 4 }];
 
const result = _.intersectionBy(arr1, arr2, 'x');
console.log(result); // Output: [{ 'x': 2 }, { 'x': 3 }]
241 chars
8 lines

The intersectionBy function takes two or more arrays as arguments and returns a new array containing the intersection of those arrays. The last argument to the function is a iteratee function used to extract the value to be compared from each element of the arrays.

In this example, arr1 and arr2 are arrays that contain objects with a property x. The intersectionBy function is called with these arrays and the iteratee function 'x' to compare objects based on their x property.

The result is a new array that contains the objects that have the same x value in both arr1 and arr2.

gistlibby LogSnag