The union
function from the Underscore library in JavaScript allows you to create an array of unique values, in order, from a combination of two or more arrays. It preserves the order of the arrays as they are passed in.
Here's an example usage of the union
function:
index.tsx222 chars10 lines
In this example, we're using the union
function to create a new array called uniqueArr
that contains all of the unique values from arr1
, arr2
, and arr3
. We pass each array as an argument to the union
function, separated by commas.
The resulting output is [1, 2, 3, 4, 5, 6, 7, 8]
. The union
function has preserved the order of the arrays as well, so the resulting array is in the same order as the arguments that were passed in.
gistlibby LogSnag