About
This dependency combines a list of arrays, returning a single array with unique values.
This can be done concisely and natively by using the Set
object and the `spread operator“:
const arr1 = [1, 2, 3, 4, 5];
const arr2 = [5, 6, 7, 8, 9];
const arr3 = [9, 10, 11, 12, 13];
const union = [...new Set([...arr1, ...arr2, ...arr3])]; // [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, …]
As a function:
function union(...arrays) {
return [...new Set(arrays.flat())];
}
const arr1 = [1, 2, 3];
const arr2 = [3, 4, 5];
const arr3 = [5, 6, 7];
const union = union(arr1, arr2, arr3); // [1, 2, 3, 4, 5, 6, 7]