Lodash _.merge() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
Object

Definition

Recursively merges own enumerable string-keyed properties of sources into the destination object. Part of Lodash Object since 0.5.0 (stable — not deprecated).

Warning: _.merge mutates the first argument and returns it. Pass _.merge({}, …sources) when you need a new object.

Syntax

Call _.merge. Start with this line.

Lodash
_.merge(object, [sources])
CallWhat it does
_.merge(object, [sources])Primary signature.
_.merge(object)Work with an object’s keys, values, or paths.

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

Library declarations
Lodash
_.merge(object, [sources])

Parameters

This method has no parameters beyond those shown in the signature.

Return Value

Return ValueDescription
*The mutated destination

Lodash Examples

Example 1 — Official: Nested Array Objects

Plain objects inside arrays are merged index by index.

Lodash
var object = { a: [{ b: 2 }, { d: 4 }] };
var other  = { a: [{ c: 3 }, { e: 5 }] };

_.merge(object, other);
// → { a: [{ b: 2, c: 3 }, { d: 4, e: 5 }] }
Try It Yourself

How It Works

  1. Key a. Both sides have an array — merge by index.
  2. Index 0. {b:2} + {c:3} → {b:2, c:3}.
  3. Index 1. {d:4} + {e:5} → {d:4, e:5}.
  4. Mutate. object itself is updated.

Example 2 — Destination Is Mutated

The first argument and the return value are the same object.

Lodash
var dest = { a: 1 };
var result = _.merge(dest, { b: 2 });

console.log(result);
// → { a: 1, b: 2 }

console.log(dest === result);
// → true
Try It Yourself

How It Works

  1. Write. Properties from the source land on dest.
  2. Return. Same reference as dest.
  3. Identity. dest === result is true.
  4. Care. Do not merge into shared defaults by accident.

Example 3 — Fresh Object Pattern

Start with {} so sources stay untouched.

Lodash
var a = { a: { x: 1 } };
var b = { a: { y: 2 }, b: 3 };

_.merge({}, a, b);
// → { a: { x: 1, y: 2 }, b: 3 }

// a and b are unchanged
Try It Yourself

How It Works

  1. Empty dest. Writes go into a new {}.
  2. Deep. Nested a.x and a.y combine.
  3. Sources. a and b are not mutated.
  4. Habit. Prefer this pattern in pure helpers.

Example 4 — Arrays Merge by Index

Not concatenation — matching indexes overwrite, extras remain.

Lodash
_.merge({ items: [1, 2, 3] }, { items: [10] });
// → { items: [10, 2, 3] }
Try It Yourself

How It Works

  1. Index 0. 1 is replaced by 10.
  2. Index 1–2. Source has no values → 2 and 3 stay.
  3. Not concat. Result length stays 3, not 4.
  4. Custom. Use _.mergeWith to concatenate.

Example 5 — merge vs assign

Deep merge keeps nested keys; shallow assign replaces the nest.

Lodash
var nested = { a: { x: 1 } };

_.assign({}, nested, { a: { y: 2 } });
// → { a: { y: 2 } }

_.merge({}, nested, { a: { y: 2 } });
// → { a: { x: 1, y: 2 } }
Try It Yourself

How It Works

  1. assign. Replaces whole a → loses x.
  2. merge. Walks into a → keeps x, adds y.
  3. Pick. Shallow overwrite → assign; nested combine → merge.
  4. Both. Here we use {} so sources stay clean.

Browser & Runtime Support

_.merge is a Lodash Object helper since 0.5.0. It runs anywhere Lodash runs — modern browsers (CDN or bundle) and Node.js / Bun / Deno with npm install lodash.

✓ Stable · Lodash 0.5.0+

_.merge()

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

100% With Lodash 0.5.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
_.merge Lodash 0.5.0+

Bottom line: Safe for production once Lodash is loaded. Prefer import merge from "lodash/merge" for smaller bundles. Remember it mutates the destination — use {} as dest when you need a fresh object.

Frequently Asked Questions

_.merge(object, …sources) recursively merges own enumerable string-keyed properties of sources into object. Official: merging nested array objects combines properties at each index.
Yes. The destination is updated in place and also returned. Use _.merge({}, a, b) when you want a fresh object.
_.assign is shallow: nested objects are replaced whole. _.merge walks into plain objects and arrays and merges them recursively.
By index, not concatenated. _.merge({items:[1,2,3]}, {items:[10]}) → {items:[10,2,3]}. For concat rules, use _.mergeWith.
undefined in a source is skipped; the destination value is kept. null overwrites like any other value.
Use import merge from "lodash/merge" or require("lodash/merge") for tree-shaking. Full build: import _ from "lodash" then _.merge.

Next: _.mergeWith()

Deep merge with a customizer for arrays and special cases.

_.mergeWith() tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

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