9951 explained code solutions for 126 technologies


javascript-lodashHow can I use Lodash to perform a deep merge in JavaScript?


Lodash is a JavaScript library that provides utility functions for common programming tasks. One of the functions it provides is a deep merge, which is used to merge two or more objects together.

The _.merge() function can be used to perform a deep merge in JavaScript. It takes two or more objects as arguments and returns a new object with all the properties from the source objects merged together.

Example

const object1 = {
  name: 'John',
  age: 20
};

const object2 = {
  name: 'Jane',
  job: 'programmer'
};

const mergedObject = _.merge(object1, object2);

console.log(mergedObject);

// Output:
// { name: 'Jane', age: 20, job: 'programmer' }

The _.merge() function will overwrite any duplicate properties with the values from the last object. It will also merge nested objects.

Parts of the code:

  • const object1 = { name: 'John', age: 20 }; - Declares an object with two properties.
  • const object2 = { name: 'Jane', job: 'programmer' }; - Declares a second object with two properties.
  • const mergedObject = _.merge(object1, object2); - Calls the _.merge() function with two objects as arguments and assigns the returned object to a new variable.
  • console.log(mergedObject); - Logs the merged object to the console.

Helpful links

Edit this code on GitHub