Lodash is trying to be helpful by recursively merging subobjects

This method is like _.assign except that it recursively merges own and inherited enumerable string keyed properties of source objects into the destination object. Source properties that resolve to undefined are skipped if a destination value exists. Array and plain object properties are merged recursively. Other objects and value types are overridden by assignment. Source objects are applied from left to right. Subsequent sources overwrite property assignments of previous sources.

https://lodash.com/docs/4.17.15#merge (emphasis mine)

If you don't want this to happen, just use the standard ES6 spread operator ... or Object.assign

const object1 = { 
  name: 'John',
  age: 23,
  books: ['book1', 'book2']
};

const object2 = { 
  name: 'John',
  age: 23,
  books: ['book3'],
  city: 'London'
};

object3 = {...object1, ...object2};
object4 = Object.assign({}, object1, object2);

console.log(object3);
console.log(object4);
/* make the Stack Overflow console bigger */ .as-console-wrapper { max-height: 100vh !important; }
Run code snippetEdit code snippet Hide Results Copy to answer Expand

Answer from Samathingamajig on Stack Overflow
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ javascript โ€บ lodash-_-merge-method
Lodash _.merge() Method - GeeksforGeeks
Lodash _.merge() method is used to merge two or more objects, starting from the left-most to the right-most to create a parent mapping object.
Published: August 5, 2025
๐ŸŒ
CoreUI
coreui.io โ€บ blog โ€บ how-to-merge-objects-in-javascript
How to Merge Objects in JavaScript ยท CoreUI
February 27, 2024 - This method goes beyond the surface, recursively merging objects at all levels, making it ideal for complex data structures. Lodashโ€™s merge() ensures that every layer of nested properties is combined, offering a level of depth that native JavaScript methods donโ€™t directly provide.
Discussions

javascript - Lodash merge two objects but replace the same key values with new object value - Stack Overflow
Array and plain object properties are merged recursively. Other objects and value types are overridden by assignment. Source objects are applied from left to right. Subsequent sources overwrite property assignments of previous sources. https://lodash.com/docs/4.17.15#merge (emphasis mine) More on stackoverflow.com
๐ŸŒ stackoverflow.com
javascript - Lodash - How to merge two arrays of objects based on key? - Stack Overflow
I want to avoid the repetition of an object with the same id...Which kind of operator can I use? ... Save this answer. ... Show activity on this post. You can get the result using merge and keyBy lodash functions. More on stackoverflow.com
๐ŸŒ stackoverflow.com
Is there a benefit to using lodash.merge (or other utilities of the same feature) versus merging via object destructuring?
Yes, absolutely. Lodash merge recursively merges properties at every level. If you merged two objects with destructuring (or lodash.assign or Object.assign which are equivalent), it drops anything that is not top-level from the first object. See: https://lodash.com/docs#merge More on reddit.com
๐ŸŒ r/node
13
4
November 14, 2022
javascript - Merge Array of Objects by Property using Lodash - Stack Overflow
Here is another way to merge two objects using Lodash: More on stackoverflow.com
๐ŸŒ stackoverflow.com
๐ŸŒ
Flavio Copes
flaviocopes.com โ€บ home โ€บ javascript โ€บ how to merge two objects in javascript
How to merge two objects in JavaScript - Flavio Copes
September 18, 2026 - This bites hard with configuration objects, where you merge user options over defaults and a whole section of the defaults silently disappears. The best solution in this case is to use Lodash and its merge() method, which will perform a deeper ...
๐ŸŒ
egghead.io
egghead.io โ€บ lessons โ€บ javascript-deep-merge-objects-in-javascript-with-spread-lodash-and-deepmerge
Deep Merge Objects in JavaScript with Spread, Lodash, and Deepmerge | egghead.io
We'll use spread here to say that we want to concatenate the two arrays together. [03:56] Now when we run that, it works. We get a deeply merged object with the pet array handled like we want. When merging objects, the spread operator works great. It's fully customizable, but it can get tedious if the objects are very deeply nested or of an unknown structure. [04:12] Lodash's merge works, but only if we want to treat arrays like objects.
Published: January 17, 2019
๐ŸŒ
Mastering JS
masteringjs.io โ€บ tutorials โ€บ lodash โ€บ merge
Lodash's `merge()` Function - Mastering JS
Given two objects destination and source, Lodash's merge() function copies the 2nd object's own properties and inherited properties into the first object.
๐ŸŒ
Dustin John Pfister
dustinpfister.github.io โ€บ 2017 โ€บ 11 โ€บ 17 โ€บ lodash_merge
_.merge in lodash as a means to recursively merge down objects. | Dustin John Pfister at github pages
November 25, 2021 - That is if a certain value is in one object, but not any other, it is the value that will end up in the final object. That is the effect that is achieved when using the lodash _.merge method. In this section I will be starting out with some simple examples of the lodash merge method. Although I will be keeping these examples very simple I assume that you have at least some experience with javaScript, and can also get to the point where you are including lodash in the javaScript environment that you are developing in.
๐ŸŒ
Tim Mousk
timmousk.com โ€บ blog โ€บ lodash-merge
How Does The Lodash Merge Function Work? โ€“ Tim Mouskhelichvili
March 6, 2023 - If two or more keys are the same, the generated object will take the value from the rightmost argument. When it comes to the bundle size, Lodash is a very big package. That's why, I recommend not using the default import, but rather importing the functions that you need individually. There are two ways of importing Lodash functions individually. ... The one-by-one import will result in the smallest bundle size. Here is a small example for you of how to use this function. javascriptimport { merge } from 'lodash'; const obj1 = { a: 'a', b: 'b' }; const obj2 = { c: 'c', d: 'd' }; const obj3 = { a: 'z', f: 'f' }; // Outputs: '{ a: "a", b: "b", c: "c", d: "d" } console.log(merge(obj1, obj2)); // Outputs: '{ a: "z", c: "c", d: "d", f: "f" }' console.log(merge(obj2, obj3)); // Outputs: '{ a: "z", b: "b", c: "c", d: "d", f: "f" }' console.log(merge(obj1, obj2, obj3));
Find elsewhere
๐ŸŒ
GitHub
gist.github.com โ€บ ahtcx โ€บ 0cd94e62691f539160b32ecda18af3d6
Deep-Merge JavaScript objects with ES6 ยท GitHub
I think the most "mind expanding: pointer I could give would be to check out what the lodash library has to offer for deep merging.The basic deep merge (https://lodash.com/docs/#merge) merges the arrays index by index, rather than append and dedupe, as shown in the example.
๐ŸŒ
Marius Schulz
mariusschulz.com โ€บ blog โ€บ combining-settings-objects-with-lodash-assign-or-merge
Combining Settings Objects with Lodash: _.assign or _.merge? โ€” Marius Schulz
July 28, 2026 - Now, the formatting object hasn't been replaced by the partial user definition, but has had its default values merged with the user settings. So, if you intend to perform a deep merge of user-provided settings with your own default values, use _.merge rather than _.assign. ES2015 brought a native Promise implementation to the JavaScript standard library.
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ lodash โ€บ lodash_merge.htm
Lodash - merge method
Array and plain object properties are merged recursively. Other objects and value types are overridden by assignment. Source objects are applied from left to right. Subsequent sources overwrite property assignments of previous sources. object (Object) โˆ’ The destination object. [sources] (...Object) โˆ’ The source objects. (Object) โˆ’ Returns object. var _ = require('lodash'); var object = { 'a': [{ 'b': 2 }, { 'd': 4 }] }; var other = { 'a': [{ 'c': 3 }, { 'e': 5 }] }; console.log(_.merge(object, other)); Save the above program in tester.js.
๐ŸŒ
JS FAQ
jsfaq.com โ€บ home โ€บ how to merge two objects in lodash
How to merge two objects in Lodash
March 7, 2026 - Master Lodash's merge function for deep object merging in JavaScript. Learn effective strategies for handling nested objects, arrays, and conflicts.
๐ŸŒ
Thesyntaxdiaries
thesyntaxdiaries.com โ€บ lodash-merge-a-comprehensive-guide
Lodash _.merge() Deep Merge Objects & Arrays โ€” Guide
February 9, 2026 - Lodash _.merge() is a JavaScript utility method that recursively deep merges properties of source objects into a destination object.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ how-to-merge-array-of-objects-by-property-using-lodash
How to Merge Array of Objects by Property using Lodash? | GeeksforGeeks
August 22, 2024 - Using the splice() method on a nested array of objects in JavaScript allows you to modify the structure of the nested array by adding or removing elements at specified positions. Table of Content Accessing the nested array directlyUtilizing ...
๐ŸŒ
Medium
medium.com โ€บ @ryan_forrester_ โ€บ javascript-combine-two-objects-quick-guide-bd37bf89d0d6
Javascript Combine Two Objects (Quick Guide) | by ryan | Medium
September 10, 2024 - Deep Merging: If you need to merge nested objects, consider using a method that supports deep merging, such as Lodashโ€™s _.merge(). The spread operator and Object.assign() only perform shallow copies.
๐ŸŒ
egghead.io
egghead.io โ€บ lessons โ€บ javascript-build-lodash-merge-from-scratch
Build lodash.merge from Scratch | egghead.io
May 26, 2020 - After getting basic types to merge we'll show off two methods for merging arrays, either using concat or recursively going through the children and merging them. Finally we'll build our own isObject function to check for object literals and recursively apply our mergeJSON function to any object literals in our source object. And last we'll add support for multiple source objects to be merged in using a for...of loop. ... The mergeJSON method we create approximates the lodash.merge functionality but does not seek to replace it.
Top answer
1 of 7
122

_.unionBy():
This method is like _.union except that it accepts iteratee which is invoked for each element of each arrays to generate the criterion by which uniqueness is computed. Result values are chosen from the first array in which the value occurs.

var original = [
  { label: 'private', value: 'private@johndoe.com' },
  { label: 'work', value: 'work@johndoe.com' }
];

var update = [
  { label: 'private', value: 'me@johndoe.com' },
  { label: 'school', value: 'schol@johndoe.com' }
];

var result = _.unionBy(update, original, "label");

console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.15.0/lodash.min.js"></script>
Run code snippetEdit code snippet Hide Results Copy to answer Expand

2 of 7
5

Convert the lists to objects keyed by label, merge them by _.assign, and convert it back to an array. It will even retain order of the items on most browsers.

var original = [
  {
    label: 'private',
    value: 'private@johndoe.com'
  },
  {
    label: 'work',
    value: 'work@johndoe.com'
  }
];

var update = [
  {
    label: 'private',
    value: 'me@johndoe.com'
  },
  {
    label: 'school',
    value: 'schol@johndoe.com'
  }
];

console.log(
  _.map(
    _.assign(
      _.mapKeys(original, v => v.label),
      _.mapKeys(update, v => v.label)
    )
  )
);


// or remove more duplicated code using spread

console.log(
  _.map(
    _.assign(
      ...[original, update].map(
        coll => _.mapKeys(coll, v => v.label)
      )
    )
  )
);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.15.0/lodash.js"></script>
Run code snippetEdit code snippet Hide Results Copy to answer Expand