Lodash _.union() method

Beginner
⏱️ 6 min read
📚 Updated: May 2026
🎯 3 Code examples
🚀 3 Try-it labs
Lodash

What you’ll learn

  • How _.union(...arrays) concatenates many inputs then collapses duplicates.
  • Why output order follows first-seen slots as arguments are consumed.
  • When specialized cousins such as unionBy or _.intersection() fit better.
  • Try each example in the editor (?tryit=1, 2, 3) with Lodash from a CDN.

Prerequisites

Knowing how _.difference() subtracts one list from another clarifies how union differs from pure subtraction workflows.

  • You understand primitive equality versus reference equality for objects.
  • You can open Try-it labs or run snippets locally.

Overview

_.union is the ergonomic deduped concat—perfect when merging permission lists, unioning CSV ids, or layering fallback arrays.

Variadic merge

Pass two lists or ten—Lodash flattens arguments uniformly before uniq-ing.

Stable survivors

Earlier arrays dominate ordering—great when primary sources should win ties.

Non-destructive

Supplied arrays remain untouched for auditing pipelines.

Syntax

javascript
_.union(...arrays)
  • arrays: two or more arrays (array-like collections are coerced consistently with other Lodash array helpers).
  • Returns: new array of unique values ordered by first appearance across the flattened arguments.
1

Two overlapping lists

Shared values appear once—position follows whichever array contributed the first copy.

javascript
import union from "lodash/union";

union([2, 3], [3, 4]);
// → [2, 3, 4]
Try it Yourself
2

Argument order shapes results

Flipping inputs changes where duplicates anchor—the earlier array wins placement.

javascript
import union from "lodash/union";

union([3, 1], [1, 2]);
// → [3, 1, 2]
Try it Yourself
3

More than two arrays

Spread any arity—Lodash still dedupes globally across the combined sequence.

javascript
import union from "lodash/union";

union([10], [20], [10, 30], [30]);
// → [10, 20, 30]
Try it Yourself

📋 _.union vs concat + uniq vs intersection

APIOutcomeMental model
_.union(...arrays)Deduped multiset mergeCombine buckets; duplicates collapse globally
a.concat(b); _.uniq(...)Similar output possibleUnion bundles flatten + uniq for fewer imports
_.intersection(...arrays)Only shared elementsOpposite goal—keep overlap instead of coverage
_.xor(...arrays)Symmetric difference flavorUse when you need toggling membership logic

Pitfalls to avoid

Obj

Reference equality

Distinct object literals never dedupe—compare ids via unionBy when merging rows.

Order

Source precedence

Shuffle inputs accidentally and priority-sensitive merges reorder unexpectedly.

Deep

Nested arrays

Union flattens one level when composing arguments—nested grids may need explicit spreads.

❓ FAQ

No. Lodash builds and returns a fresh array; each argument stays untouched.
It relies on SameValueZero semantics—matching Lodash uniq—including treating NaN as equal to NaN.
The earliest occurrence in argument order wins; later duplicates are skipped.
Yes. Union accepts any number of arrays (variadic) and flattens them one level before uniq-ing.
Use import union from "lodash/union" or require("lodash/union") for tree-shaken bundles.

Summary

  • Purpose: _.union(...arrays) merges arrays then removes duplicates with SameValueZero rules.
  • Ordering: earliest appearances—in argument order—determine final sequencing.
  • Next: Lodash _.unionBy(), _.takeWhile() (previous), or the array methods hub.
Did you know?

When duplicates differ only after an iteratee—say merging user rows by email—use _.unionBy(); raw union compares elements directly with SameValueZero.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

6 people found this page helpful