Array/Since
1.38.0union
Combines two arrays into a new array containing only unique elements.
union(array, values):T[]Parameters
arrayT[]requiredThe first array whose elements should be included in the result.
valuesT[]requiredThe second array whose elements should be combined with the first array.
Returns
T[]A new array containing the unique elements from both input arrays.
Examples
01
Basic usage
Combine two arrays and remove duplicate values.
TypeScript
import { union } from "@cyref/js";
const array = [1, 2, 3];
const values = [3, 4, 5];
const result = union(array, values);
console.log(result);
// [1, 2, 3, 4, 5]02
Duplicate values
Duplicate values are removed from the combined result.
TypeScript
import { union } from "@cyref/js";
const array = [1, 2, 2, 3];
const values = [2, 3, 3, 4];
const result = union(array, values);
console.log(result);
// [1, 2, 3, 4]03
Strings
The function can combine arrays of strings while removing duplicate values.
TypeScript
import { union } from "@cyref/js";
const array = ["JavaScript", "TypeScript"];
const values = ["TypeScript", "Python"];
const result = union(array, values);
console.log(result);
// ["JavaScript", "TypeScript", "Python"]04
Empty arrays
Empty arrays are handled naturally when combining the values.
TypeScript
import { union } from "@cyref/js";
const result = union([], [1, 2, 3]);
console.log(result);
// [1, 2, 3]05
Both arrays empty
When both input arrays are empty, the function returns an empty array.
TypeScript
import { union } from "@cyref/js";
const result = union([], []);
console.log(result);
// []Built with@cyref/js·Official documentation