Open In App

Lodash _.merge() Method

Last Updated : 05 Aug, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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. When two keys are the same, the generated object will have a value for the rightmost key.

If more than one object is the same, the newly generated object will have only one key (most right side) and value corresponding to those objects.

Note: It modifies the original (target) object directly.

Syntax

_.merge(object, [sources])

Parameters:

  • object parameter holds the destination object.
  • sources parameter holds the source object. It is an optional parameter.

Return Value: This method returns the merged object.

Let's now understand _.merge() Method with an example

Here, when two keys are the same, then only the right-most key will be added to a new array, and if more than two objects are the same, then it will add only one.

JavaScript
// Requiring the lodash library  
const _ = require("lodash");

// Using the _.merge() method 
console.log(
    _.merge({ cpp: "12" }, { java: "23" },
        { python: "35" })
);

// When two keys are the same
console.log(
    _.merge({ cpp: "12" }, { cpp: "23" },
        { java: "23" }, { python: "35" })
);

// When more than one object is the same
console.log(
    _.merge({ cpp: "12" }, { cpp: "12" },
        { java: "23" }, { python: "35" })
);

In this example

  • The code uses Lodash's _.merge() to combine JavaScript objects.
  • The first example merges three objects with different keys, creating a single object with all three key-value pairs.
  • The second example shows that when the same key (cpp) exists in multiple objects, the value from the last object encountered is used in the merged result.
  • The third example also has a repeated key (cpp), and again, the final value is determined by the last object containing that key.
  • Each console.log displays the result of merging the given objects, illustrating how _.merge() handles unique and duplicate keys.

Output

{ cpp: '12', java: '23', python: '35' }
{ cpp: '23', java: '23', python: '35' }
{ cpp: '12', java: '23', python: '35' }

How Does Lodash _.merge() Work?

  • Imagine you have a few lists (objects), and you want to combine them into one big list. _.merge() helps you do that.
  • It starts with the first list and adds the items from the next ones in order. If something already exists in the first list, it will be replaced by the item from the last list.
  • If the same item appears in multiple lists, the item from the last list (rightmost) will be kept.
  • If one of the lists has smaller boxes (nested objects), it opens those boxes and merges everything inside, not just on the top level.
  • The first list you give to _.merge() gets updated directly, instead of creating a completely new list.

Now let's understand _.merge() method with one more example

When we are merging two different objects then it will merge recursively (one right-most element from 'other' and one right-most element from 'object').

JavaScript
// Requiring the lodash library  
const _ = require("lodash");

// The destination object
let object = {
    'amit': [{ 'susanta': 20 }, { 'durgam': 40 }]
};

// The source object
let other = {
    'amit': [{ 'chinmoy': 30 }, { 'kripamoy': 50 }]
};

// Using the _.merge() method
console.log(_.merge(object, other));

In this example:

  • It starts with a object containing a list under the key 'amit'.
  • It has another other object, also with a list under 'amit'.
  • _.merge() combines these two objects.
  • Instead of just overwriting the 'amit' list, it merges the arrays inside.
  • The final result will have 'amit' containing a combined list of all the inner objects from both the original object and other

Output:

{
    amit: [ { susanta: 20, chinmoy: 30 }, { durgam: 40, kripamoy: 50 } ]
}

Use Case of Lodash _.merge() Method

The Lodash _.merge() method is primarily used when you need to combine multiple objects, especially when dealing with nested structures, ensuring that both top-level properties and deep properties (within objects or arrays) are merged properly.

A common use case for _.merge() is combining configuration settings from different sources. For example, when you have:

  • Default Settings: The initial global settings that apply to everyone.
  • User Settings: Preferences that a user has customized for their own experience.
  • Environment Settings: Settings that are specific to the environment (development, production, etc.).

Lodash _.merge() Method

Similar Reads