how to use the union function from the underscore library in typescript

To use the union function from the Underscore library in Typescript, you will first need to install Underscore through npm:

index.ts
npm install underscore
23 chars
2 lines

Next, you can import the union function from the Underscore module in your Typescript file:

index.ts
import { union } from 'underscore';
36 chars
2 lines

Now you can use the union function to create a union array of two or more arrays, like this:

index.ts
const arr1: number[] = [1, 2, 3];
const arr2: number[] = [2, 3, 4];
const arr3: number[] = [4, 5, 6];

const unionArr: number[] = union(arr1, arr2, arr3);
console.log(unionArr); // Output: [1, 2, 3, 4, 5, 6]
208 chars
7 lines

The union function takes two or more arrays as arguments and returns a new array with unique values from all the arrays. The returned array will contain only one occurrence of each value, even if that value is present in multiple input arrays.

gistlibby LogSnag