Skip to main content

union()

union<T>(arrays): T[]

Creates an array of unique values from all given arrays using SameValueZero for equality.

note

First occurrence wins when duplicates are found.


Type Parametersโ€‹

T: Tโ€‹

The type of elements in the arrays.


Parametersโ€‹

arrays: readonly (readonly T[])[]โ€‹

The arrays to combine.


Returns: T[]โ€‹

A new array with unique elements.


Sinceโ€‹

2.0.0


Performanceโ€‹

O(n) โ€” uses Set for constant-time lookups.


Also known asโ€‹

union (Lodash, es-toolkit, Remeda, Ramda, Effect) ยท unique (Radashi) ยท โŒ (Modern Dash, Antfu)


Exampleโ€‹

union([[1, 2], [2, 3], [3, 4]]);
// => [1, 2, 3, 4]

union([['a', 'b'], ['b', 'c']]);
// => ['a', 'b', 'c']

How it works?โ€‹

Combines multiple arrays into one with unique values. First occurrence wins โ€” preserves order of first appearance.


Use Casesโ€‹

Combine arrays without duplicates ๐Ÿ“Œโ€‹

Merge multiple arrays into one with unique values. Perfect for merging lists, combining selections, or deduplicating data.

const team1 = ["Alice", "Bob", "Charlie"];
const team2 = ["Bob", "Diana", "Eve"];
const team3 = ["Alice", "Frank"];

const allMembers = union(team1, team2, team3);
// => ["Alice", "Bob", "Charlie", "Diana", "Eve", "Frank"]

Merge tag listsโ€‹

Combine tags from multiple sources into a unique set. Useful for content aggregation or search indexing.

const userTags = ["javascript", "react"];
const articleTags = ["react", "typescript", "nodejs"];
const projectTags = ["typescript", "testing"];

const allTags = union(userTags, articleTags, projectTags);
// => ["javascript", "react", "typescript", "nodejs", "testing"]

Aggregate permissionsโ€‹

Combine permissions from multiple roles. Essential for access control systems.

const adminPerms = ["read", "write", "delete"];
const editorPerms = ["read", "write", "publish"];

const combinedPerms = union(adminPerms, editorPerms);
// => ["read", "write", "delete", "publish"]