how to use the union function from lodash in javascript

To use the union function from lodash in javascript, we first need to install it with npm:

npm install lodash
19 chars
2 lines

Then, we can include it in our javascript code with require:

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

Now, we can use the union function to combine arrays and remove duplicates:

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

const unionArr = _.union(arr1, arr2, arr3);
console.log(unionArr); // [1, 2, 3, 4, 5]
159 chars
7 lines

The union function takes multiple arrays as arguments and returns a new array with all unique values from each array, in the order that they appear.

gistlibby LogSnag