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 Answer from thinkmatt on reddit.com
🌐
Lodash
lodash.com › docs
Lodash Documentation
The iteratee is invoked with four arguments: (accumulator, value, index|key, collection). Many lodash methods are guarded to work as iteratees for methods like _.reduce, _.reduceRight, and _.transform. The guarded methods are: assign, defaults, defaultsDeep, includes, merge, orderBy, and sortBy
🌐
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
Discussions

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
Deep Merge using Lodash
Now I want to merge these arrays ... and merge only the properties from the new address that are actually present. So the result should look like this: var result = [ { label: 'home', address: { city: 'London (Central)', zipCode: '12345', country: 'UK' } }, { label: 'work', address: { city: 'New York', zipCode: '54321' } }, { label: 'spain', address: { city: 'Madrid', zipCode: '55555' } } ] How can I do this using lodash... More on stackoverflow.com
🌐 stackoverflow.com
Lodash merge including undefined values
I'm trying to use Lodash to merge object A into object B, but the trouble I am having is that object A has some undefined values and I want these to be copied over to object B. Lodash docs for ... More on github.com
🌐 github.com
3
August 26, 2021
Lodash merging and unioning of nested array / object structure
I'm just struggling with the logic sequence process here and need a nudge to figure out what tools to use. Currently I'm trying to work with Lodash's _.merge and _.union in some combination. It seems I can use _.mergeWith or _.unionBy to "nest" the merging steps without resorting to manually ... More on stackoverflow.com
🌐 stackoverflow.com
🌐
npm
npmjs.com › package › lodash.merge
lodash.merge - npm
The Lodash method `_.merge` exported as a module.. Latest version: 4.6.2, last published: 7 years ago. Start using lodash.merge in your project by running `npm i lodash.merge`. There are 7028 other projects in the npm registry using lodash.merge.
      » npm install lodash.merge
    
Published   Jul 10, 2019
Version   4.6.2
Author   John-David Dalton
🌐
GitHub
github.com › lodash › lodash
lodash v4.18.1
As announced on the OpenJS Foundation blog, Lodash has received support from the Sovereign Tech Agency and will transition to the Feature-Complete maturity stage so that it remains stable, secure, and sustainable long-term. As part of this effort, Lodash is rebooting its governance.
Starred by 61.3K users
Forked by 7.1K users
Languages   JavaScript 97.2% | HTML 2.3% | EJS 0.5%
Top answer
1 of 3
59

You can turn both arrays into objects using _.keyBy(arr, 'label'), and then merge deep using _.merge():

var originalAddresses = [{
  label: 'home',
  address: {
    city: 'London',
    zipCode: '12345'
  }
}, {
  label: 'work',
  address: {
    city: 'New York',
    zipCode: '54321'
  }
}];

var updatedAddresses = [{
  label: 'home',
  address: {
    city: 'London (Central)',
    country: 'UK'
  }
}, {
  label: 'spain',
  address: {
    city: 'Madrid',
    zipCode: '55555'
  }
}];

var result = _.values(_.merge(
  _.keyBy(originalAddresses, 'label'),
  _.keyBy(updatedAddresses, 'label')
));

console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.15.0/lodash.min.js"></script>

2 of 3
1

I try to use VanillaJS to handle this.

const originalAddresses = [{
    label: 'home',
    address: {
        city: 'London',
        zipCode: '12345'
    }
}, {
    label: 'work',
    address: {
        city: 'New York',
        zipCode: '54321'
    }
}];

const updatedAddresses = [{
    label: 'home',
    address: {
        city: 'London (Central)',
        country: 'UK'
    }
}, {
    label: 'spain',
    address: {
        city: 'Madrid',
        zipCode: '55555'
    }
}];


const groupBy = (array, property) => {
    return array.reduce((acc, cur) => {
            let key = cur[property]
            if (!acc[key]) {
                acc[key] = []
            }
            acc[key].push(cur)

            return acc
        }
        , {})
}

const groupByLabel = groupBy([...originalAddresses, ...updatedAddresses], 'label')

const result  = Object.keys(groupByLabel).map((key) => {
        return {
            label: groupByLabel[key][0].label,
            address: groupByLabel[key].reduce((acc, cur) => {
                    return Object.assign(acc, cur.address)
                }
                , {})
        }
    }
)
console.log(result)

🌐
Marius Schulz
mariusschulz.com › blog › combining-settings-objects-with-lodash-assign-or-merge
Combining Settings Objects with Lodash: _.assign or _.merge? — Marius Schulz
November 1, 2020 - In the end, we'd like the merged ... functions, _.assign and _.merge, that assign property values of some source object(s) to a target object, effectively merging their properties....
Find elsewhere
🌐
Es-toolkit
es-toolkit.dev › reference › compat › object › merge.html
merge (Lodash compatibility) | es-toolkit
3 weeks ago - Deeply merges one or more source objects into the target object. Nested objects and arrays are recursively merged. If a source object property is undefined, it won't overwrite the existing value in the target object.
🌐
GitHub
github.com › lodash › lodash › issues › 5242
Lodash merge including undefined values · Issue #5242 · lodash/lodash
August 26, 2021 - I'm trying to use Lodash to merge object A into object B, but the trouble I am having is that object A has some undefined values and I want these to be copied over to object B. Lodash docs for _.merge() says: "Recursively merges own enum...
Author   liangyuqi
🌐
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 - I want everything merged down together in a way where the values in the input object will override any default values, and I want the this keyword in the methods object to refer to the values in the resulting object of an object that is created from all of this. This differs from other methods that might copy inputs.delta over in a way in which I will end up with an undefined value for delta.y, this is the case with _.assign or Object.assign. So the lodash merge method is one such method that seems to be something of use compared to what is available in just native javaScript by itself at least at the time of this writing.
🌐
Lodash
lodash.com
Lodash
Lodash makes JavaScript easier by taking the hassle out of working with arrays, numbers, objects, strings, etc.
Top answer
1 of 3
2

The complexity with this problem is that you want to merge on 2 different layers:

  • you want to merge two arrays of towns, so you need to decide what to do with towns common to the two arrays;
  • when handling two towns with common name, you want to merge their occupants.

Now, both _.merge and _.mergeWith are good candidates to accomplish the task, except that they are for operating on objects (or associative maps, if you like), whereas you have vectors of pairs (well, not really pairs, but objects with two elements with fixed keys; name/status and townName/occupants are fundamentally key/value) at both layers mentioned above.

One function that can be useful in this case is one that turns an array of pairs into an object. Here's such a utility:

arrOfPairs2Obj = (k, v) => (arr) => _.zipObject(..._.unzip(_.map(arr, _.over([k, v])))); 

Try executing the following

townArr2townMap = arrOfPairs2Obj('townName', 'occupants');
mapA = townArr2townMap(arrayA);
mapB = townArr2townMap(arrayB);

to see what it does.

Now you can merge mapA and mapB more easily…

_.mergeWith(mapA, mapB, (a, b) => {
    // … well, not that easily
})

Again, a and b are arrays of "pairs" name/status, so we can reuse the abstraction I showed above, defining

personArr2personMap = arrOfPairs2Obj('name', 'status');

and using it on a and b.

But still, there are some problems. I thought that the (a, b) => { … } I wrote above would be called by _.mergeWith only for elements which have the same key across mapA and mapB, but that doesn't seem to be the case, as you can verify by running this line

_.mergeWith({a: 1, b: 3}, {b:2, c:4, d: 6}, (x, y) => [x, y])

which results in

{
  a: 1
  b: [3, 2]
  c: [undefined, 4]
  d: [undefined, 6]
}

revealing that the working lambda is called for the "clashing" keys (in the case above just b), and also for the keys which are absent in the first object (in the case above c and d), but not for those absent in the second object (in the case above a).

This is a bit unfortunate, because, while you could filter dead people out of towns which are only in arrayB, and you could also filter out those people which are dead in arrayB while alive in arrayA, you'd still have no place to filter dead people out of towns which are only in arrayA.

But let's see how far we can get. _.merge doc reads

Source objects are applied from left to right. Subsequent sources overwrite property assignments of previous sources.

So we can at least handle the merging of towns common across the array in a more straightforward way. Using _.merge means that if a person is common in the two arrays, we'll always pick the one from arrayB, whether that's (still) alive or (just) dead.

Indeed, a strategy like this doesn't give you the precise solution you want, but not even one too far from it,

notSoGoodResult = _.mergeWith(mapA, mapB, (a, b) => {
   return _.merge(personArr2personMap(a), personArr2personMap(b));
})

its result being the following

{
  town1: [
    {name: "Charlie", status: "alive"},
    {name: "Jim", status: "dead"}
  ],
  town2: [
    {name: "Rachel", status: "alive"}
  ],
  town3:
    Alice: "alive",
    Bob: "dead",
    Joe: "alive"
  },
  town5: {
    Bob: "alive",
    Ray: "alive",
    Sam: "dead"
  }
}

As you can see

  • Bob in town3 is correctly dead,
  • we've not forgotten Alice in town3,
  • nor have we forogtten about Joe in town3.

What is left to do is

  • "reshaping" town3 and town5 to look like town1 and town2 (or alternatively doing the opposite),
  • filtering away all dead people (there's no more people appearing with both the dead and alive status, so you don't risk zombies).

Now I don't have time to finish up this, but I guess the above should help you in the right direction.


The bottom line, however, in my opinion, is that JavaScript, even with the power of Lodash, is not exactly the best tool for functional programming. _.mergeWith disappointed me, for the reason explained above.


Also, I want to mention that there a module named lodash/fp that

promotes a more functional programming (FP) friendly style by exporting an instance of lodash with its methods wrapped to produce immutable auto-curried iteratee-first data-last methods.

This shuould slightly help you be less verbose. With reference to your self answer, and assuming you wanted to write the lambda

person => {return person.status == "alive";}

in a more functional style, with "normal" Lodash you'd write

_.flowRight([_.curry(_.isEqual)('alive'), _.iteratee('status')])

whereas with lodash/fp you'd write

_.compose(_.isEqual('alive'), _.get('status'))
2 of 3
1

You can define a function for merging arrays with a mapper like this:

  const union = (a1, a2, id, merge) => {
    const dict = _.fromPairs(a1.map((v, p) => [id(v), p]))
    return a2.reduce((a1, v) => {
      const i = dict[id(v)]
      if (i === undefined) return [...a1, v] 
      return Object.assign([...a1], { [i]: merge(a1[i], v) })
    }, a1)
  }

and use it like this:

union(
  arrayA, 
  arrayB, 
  town => town.townName, 
  (town1, town2) => ({
    ...town1,
    occupants: union(
      town1.occupants,
      town2.occupants,
      occupant => occupant.name,
      (occupant1, occupant2) => occupant1.status === 'alive' ? occupant1 : occupant2
    ).filter(occupant => occupant.status === 'alive')
  })
)
🌐
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 ...
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › lodash-_-mergewith-method
Lodash _.mergeWith() Method - GeeksforGeeks
July 15, 2025 - Lodash _.mergeWith() method uses a customizer function that is invoked to produce the merged values of the given destination and source properties. When the customizer function returns undefined, the merging is handled by the method instead.
🌐
GitHub
github.com › lodash › lodash › issues › 2872
[Question] merge does not concat arrays? · Issue #2872 · lodash/lodash
December 7, 2016 - const a = ['a']; const b = ['b']; console.log(_.merge(a, b)); // expected: ['a', 'b'], actual: ['b']
Author   Sharaal
🌐
The Syntax Diaries
thesyntaxdiaries.com › lodash-merge-a-comprehensive-guide
Lodash _.merge() Deep Merge Objects & Arrays — Guide with Examples
February 9, 2026 - Lodash _.merge() is a JavaScript utility method that recursively deep merges properties of source objects into a destination object.
🌐
Cloudsmith
cloudsmith.com › navigator › npm › lodash.merge
lodash.merge (4.6.2) - npm Package Quality | Cloudsmith Navigator
Learn all about the quality, security, and current maintenance status of lodash.merge using Cloudsmith Navigator
🌐
Tim Mousk
timmousk.com › blog › lodash-merge
How Does The Lodash Merge Function Work? – Tim Mouskhelichvili
March 6, 2023 - The Lodash merge function merges two or more objects into one.
🌐
NPM Compare
npm-compare.com › deepmerge,lodash.merge,merge-deep,merge-options
lodash.merge vs deepmerge vs merge-options vs merge-deep
Lodash.merge performs a recursive merge of properties, allowing you to combine objects while handling arrays and other data types. If you are already using Lodash in your project, lodash.merge can be a convenient option for merging objects. To see how deepmerge compares with immer and lodash.merge, ...