Lodash _.unionWith() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
Array

Definition

Like _.union, except it accepts a comparator to decide when two elements are duplicates. Called as (arrVal, othVal). Order and references come from the first occurrence. Since Lodash 4.0.0.

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

Syntax

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

Lodash
_.unionWith([arrays], [comparator])
CallWhat it does
_.unionWith([arrays], [comparator])Primary signature.
_.unionWith(array)Call with an array argument.

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

Library declarations
Lodash
_.unionWith([arrays], [comparator])

Parameters

ParameterDescription
…arraysOptional. Arrays to merge (*empty call → [])
comparatorOptional. (arrVal, othVal) ⇒ boolean (*omit ≈ plain union)

Return Value

Return ValueDescription
*The value returned by _.unionWith().

Lodash Examples

Example 1 — Official: Deep Equal With _.isEqual

Merge object arrays; deep-equal duplicates are skipped.

Lodash
var objects = [{ x: 1, y: 2 }, { x: 2, y: 1 }];
var others = [{ x: 1, y: 1 }, { x: 1, y: 2 }];

_.unionWith(objects, others, _.isEqual);
// → [{ x: 1, y: 2 }, { x: 2, y: 1 }, { x: 1, y: 1 }]
Try It Yourself

How It Works

  1. Start. Take both objects from the first array.
  2. Add. { x: 1, y: 1 } is new → keep.
  3. Skip. Second { x: 1, y: 2 } deep-equals the first → drop.
  4. Order. Survivors follow first-seen order.

Example 2 — Custom Field Comparator

Treat objects as equal when both x and y match.

Lodash
function sameXY(a, b) {
  return a.x === b.x && a.y === b.y;
}

_.unionWith(
  [{ x: 1, y: 2, tag: "a" }],
  [{ x: 1, y: 2, tag: "b" }, { x: 3, y: 4 }],
  sameXY
);
// → [{ x: 1, y: 2, tag: "a" }, { x: 3, y: 4 }]
Try It Yourself

How It Works

  1. Rule. Equal if x and y both match.
  2. Keep first. tag: "a" wins for coordinates (1,2).
  3. Skip. tag: "b" is a duplicate under that rule.
  4. Add. (3,4) is new → append.

Example 3 — Numeric Tolerance

Numbers within 0.1 count as the same.

Lodash
function near(a, b) {
  return Math.abs(a - b) < 0.1;
}

_.unionWith([1.0, 2.0], [1.05, 3.0], near);
// → [1, 2, 3]
Try It Yourself

How It Works

  1. Keep. 1 and 2 from the first array.
  2. Skip. 1.05 is near 1 → duplicate.
  3. Add. 3 is far from both → keep.
  4. Use. Floating-point merges where exact equality fails.

Example 4 — unionWith vs unionBy

By projects one key; With decides equality with any rule.

Lodash
var a = [{ x: 1, y: 9 }];
var b = [{ x: 1, y: 0 }];

_.unionBy(a, b, "x");
// → [{ x: 1, y: 9 }]     // same x → skip second

_.unionWith(a, b, _.isEqual);
// → both kept            // full objects differ
Try It Yourself

How It Works

  1. By. Same x → second object dropped.
  2. With + isEqual. y differs → both kept.
  3. Pick By when one field defines identity.
  4. Pick With when equality is richer than a single key.

Example 5 — First Occurrence Wins

Earlier arrays decide which duplicate reference is kept.

Lodash
var first = [{ id: 1, name: "Ada" }];
var second = [{ id: 1, name: "Updated" }];

_.unionWith(first, second, function (a, b) {
  return a.id === b.id;
});
// → [{ id: 1, name: "Ada" }]
Try It Yourself

How It Works

  1. Match. Same id → treated as duplicates.
  2. Keep first. name: "Ada" from the earlier array wins.
  3. Skip later. "Updated" never enters the result.
  4. Tip. Put preferred sources first when merging.

Browser & Runtime Support

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

✓ Stable · Lodash 4.0+

_.unionWith()

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

100% With Lodash 4.x
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
_.unionWith Lodash 4.0+

Bottom line: Safe for production once Lodash is loaded. Prefer import unionWith from "lodash/unionWith" for smaller builds. Not a native browser API — load Lodash first.

Frequently Asked Questions

It merges multiple arrays into one unique list, using a custom comparator (arrVal, othVal) to decide when two elements are duplicates. Order and references follow the first occurrence across the combined arrays.
No. It returns a new array. Source arrays are unchanged.
Return a truthy value when the two arguments should count as the same element (a duplicate). Return falsy when they are distinct.
The earliest element in combined argument order wins. Later matches are skipped.
Use unionBy when one projected key or transform is enough (for example "id"). Use unionWith for deep equality, tolerances, or multi-field rules.
Use import unionWith from "lodash/unionWith" or require("lodash/unionWith") for tree-shaking. Full build: import _ from "lodash" then _.unionWith.

Next: _.uniq()

Create a duplicate-free version of an array.

_.uniq() 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.

4 people found this page helpful