how to use the intersection function from lodash in javascript

To use the intersection function from lodash in JavaScript, first, we need to include the lodash library in our project. We can do this by installing it via npm:

index.tsx
npm install lodash
19 chars
2 lines

Then, we can import it into our JavaScript file:

index.tsx
const _ = require('lodash');
29 chars
2 lines

With lodash imported, we can use the _.intersection() function, which returns an array of unique values that are included in all given arrays.

Here's an example:

index.tsx
const arr1 = [1, 2, 3];
const arr2 = [2, 3, 4];
const arr3 = [3, 4, 5];

const result = _.intersection(arr1, arr2, arr3);
console.log(result); // Output: [3]
158 chars
7 lines

In this example, _.intersection() takes three arrays as parameters and returns an array containing only the value 3, which is the only value that exists in all three arrays.

gistlibby LogSnag