Lodash _.unionBy() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
Array

Definition

Like _.union, but uniqueness is computed from an iteratee applied to each element. The first element seen for each projected key is kept. Part of Lodash Array since 4.0.0 (stable — not deprecated).

Pass the required arguments shown below. Lodash Array helpers usually return a new value — check whether the source array is mutated.

Syntax

Call _.unionBy on an array. Start with this line.

Lodash
_.unionBy([arrays], [iteratee=_.identity])
CallWhat it does
_.unionBy([arrays], [iteratee=_.identity])Primary signature.
_.unionBy(array)Call with an array argument.

Prefer import unionBy from "lodash/unionBy" when you want a smaller bundle.

Library declarations
Lodash
_.unionBy([arrays], [iteratee=_.identity])

Parameters

ParameterDescription
arraysOptional. Sources to merge; scanned in argument order
iterateeOptional. Criterion per element; default identity; always last

Return Value

Return ValueDescription
*New array of first-seen elements per key

Lodash Examples

Example 1 — Official: Math.floor

Floats collapse when their floors match; the first value wins.

Lodash
_.unionBy([2.1], [1.2, 2.3], Math.floor);
// → [2.1, 1.2]
Try It Yourself

How It Works

  1. Project. floor(2.1)=2 → keep 2.1.
  2. New key. floor(1.2)=1 → keep 1.2.
  3. Duplicate key. floor(2.3)=2 already seen → skip.
  4. Iteratee last. Math.floor is the final argument.

Example 2 — Official: Property Shorthand

Merge objects uniquely by a field name string.

Lodash
_.unionBy([{ x: 1 }], [{ x: 2 }, { x: 1 }], "x");
// → [{ x: 1 }, { x: 2 }]
Try It Yourself

How It Works

  1. Shorthand. "x" reads each object’s x property.
  2. Keep first. { x: 1 } from the left array is kept.
  3. New key. { x: 2 } is new → kept.
  4. Skip duplicate. Later { x: 1 } shares key 1 → skipped.

Example 3 — Merge Users by id

Typical API use: combine lists; keep the first row per id.

Lodash
var a = [{ id: 1, name: "Ada" }, { id: 2, name: "Bob" }];
var b = [{ id: 2, name: "Bobby" }, { id: 3, name: "Cy" }];

_.unionBy(a, b, "id");
// → [{ id: 1, name: "Ada" }, { id: 2, name: "Bob" }, { id: 3, name: "Cy" }]
Try It Yourself

How It Works

  1. Key. Compare on id only.
  2. From a. Keep Ada (1) and Bob (2).
  3. Collision. Bobby has id: 2 — already seen → skip.
  4. New. Cy (3) is appended from list b.

Example 4 — Three Arrays

Pass as many arrays as you need; the iteratee still comes last.

Lodash
_.unionBy([2.1], [1.2, 2.3], [2.4, 3.1], Math.floor);
// → [2.1, 1.2, 3.1]
Try It Yourself

How It Works

  1. Keys so far. Keep floors 2 and 1 from the first two lists.
  2. Skip 2.4. Floor 2 already taken by 2.1.
  3. Keep 3.1. Floor 3 is new.
  4. Rule. More arrays only add values with unseen keys.

Example 5 — vs Plain _.union

Without an iteratee, different object instances never collapse.

Lodash
var left = [{ x: 1 }];
var right = [{ x: 1 }];

_.union(left, right);
// → [{ x: 1 }, { x: 1 }]   // two instances

_.unionBy(left, right, "x");
// → [{ x: 1 }]             // same key → one row
Try It Yourself

How It Works

  1. Plain union. Compares by reference — two objects stay.
  2. unionBy. Both project to key 1 — second is skipped.
  3. When to switch. Use unionBy for records, emails, or ids.
  4. Custom logic. Prefer _.unionWith for pair-wise comparators.

Browser & Runtime Support

_.unionBy is a Lodash 4.x Array helper (added in 4.0.0). It runs anywhere Lodash runs — modern browsers (via CDN or bundle) and Node.js / Bun / Deno with npm install lodash.

✓ Stable · Lodash 4.0.0+

_.unionBy()

Available in [email protected] full builds and the lodash/unionBy modular import. Works in evergreen browsers and current Node LTS when Lodash is installed.

100% With Lodash 4.0.0+
Google Chrome Supported · Desktop & Mobile
Full support
Mozilla Firefox Supported · Desktop & Mobile
Full support
Apple Safari Supported · macOS & iOS
Full support
Microsoft Edge Supported · Chromium
Full support
Internet Explorer No native support · Use a polyfill
Polyfill
Opera Supported · Modern versions
Full support
Samsung Internet Supported · Android
Full support
Bun Supported · JavaScript runtime
Supported
Deno Supported · JavaScript runtime
Supported
Node.js Supported · Server runtime
Supported
Android WebView Supported · Modern WebView
Full support
_.unionBy Lodash 4.0.0+

Bottom line: Safe for production once Lodash 4.0.0+ is on the page or in your bundle. Prefer import unionBy from "lodash/unionBy" for smaller builds. For plain equality without an iteratee, use _.union.

Frequently Asked Questions

_.unionBy(...arrays, iteratee) is like _.union, but uniqueness is computed from iteratee(value). The first element seen for each projected key is kept. Order follows first-seen discovery across arguments.
No. It returns a new array. Each argument stays unchanged.
Last. Pass every array first, then the iteratee — same pattern as _.intersectionBy and other *By helpers.
Yes. A string like "x" or "id" is the _.property shorthand — compare objects by that field.
uniqBy dedupes one array by iteratee. unionBy merges many arrays first, then keeps the first element per projected key.
Use import unionBy from "lodash/unionBy" or require("lodash/unionBy") for tree-shaking friendly bundles. Full build: import _ from "lodash" then _.unionBy.

Next: _.unionWith()

Merge arrays uniquely with a custom comparator function.

_.unionWith() tutorial →

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.

10 people found this page helpful