how to use the intersection function from the underscore library in javascript

To use the intersection function from the underscore library, first install the library using npm:

npm install underscore
23 chars
2 lines

Then require the underscore module in your javascript file:

index.tsx
const _ = require('underscore');
33 chars
2 lines

The intersection function takes in two or more arrays and returns a new array containing the unique values that are present in all of the input arrays. Here is an example usage:

index.tsx
const array1 = [1, 2, 3, 4];
const array2 = [3, 4, 5, 6];
const array3 = [4, 5, 6, 7];

const intersected = _.intersection(array1, array2, array3);

console.log(intersected); // Output: [4]
190 chars
8 lines

In this example, the intersection function is called with three arrays as input (array1, array2, and array3). The output is a new array containing the intersection of these arrays, which is [4].

gistlibby LogSnag