Table of contents
1.
Introduction
2.
Why This Function is Used
3.
Syntax, Parameter and Return Value
3.1.
Syntax: 
3.2.
Parameters:
3.3.
Return Value: 
4.
Examples 
4.1.
Customizing Property Assignment:
4.2.
JavaScript
4.3.
Handling Property Conflicts:
4.4.
JavaScript
4.5.
Transforming Assigned Values:
4.6.
JavaScript
4.7.
Merging Objects with Priority:
4.8.
JavaScript
5.
Frequently Asked Questions
5.1.
How does _.extendWith() differ from _.assignWith()?
5.2.
Is _.extendWith() suitable for deep merging of objects?
5.3.
What happens in case of property name conflicts?
6.
Conclusion
Last Updated: Mar 27, 2024
Easy

Lodash _.extendWith() Method

Author Riya Singh
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Merging objects in JavaScript, particularly when dealing with complex or nested objects, can sometimes require specific rules for how properties are combined or overwritten. Lodash's _.extendWith() method, an alias for _.assignInWith(), addresses this need. It copies own and inherited enumerable string keyed properties from source objects to the target object, but with the addition of a customizer function that dictates how these values are assigned or merged. 

Lodash _.extendWith() Method

This method is particularly useful in scenarios where you need a customized approach to property assignment.

Why This Function is Used

The _.extendWith() (or _.assignInWith()) function is used for merging properties, including inherited ones, from source objects into a target object with the ability to customize how these properties are assigned. This method is crucial in situations where the default behavior of object merging doesn't suffice, and specific rules are needed to handle conflicts, transform values, or apply special logic during the assignment process.

Syntax, Parameter and Return Value

Syntax: 

_.extendWith(target, ...sources, customizer)

Parameters:

  • target (Object): The destination object.
     
  • ...sources (Object): The source objects.
     
  • customizer (Function): The function used to customize assigned values.

Return Value: 

(Object) - Returns the modified target object.

Examples 

Customizing Property Assignment:

  • JavaScript

JavaScript

var _ = require('lodash');

function customizer(objValue, srcValue) {

 if (_.isArray(objValue)) {

   return objValue.concat(srcValue);

 }

}

var object = { 'a': [1], 'b': [2] };

var other = { 'a': [3], 'b': [4] };

_.extendWith(object, other, customizer);

console.log(object);
You can also try this code with Online Javascript Compiler
Run Code

 Output: 

{ 'a': [1, 3], 'b': [2, 4] }


Demonstrates using a customizer function to concatenate array values from source objects.

Handling Property Conflicts:

  • JavaScript

JavaScript

var target = { 'prop1': 'value1' };

var source = { 'prop1': 'value2', 'prop2': 'value3' };

_.extendWith(target, source, (objVal, srcVal) => {

 return objVal ? objVal : srcVal;

});

console.log(target);
You can also try this code with Online Javascript Compiler
Run Code

Output: 

{ 'prop1': 'value1', 'prop2': 'value3' }


Shows how to handle property conflicts by preferring the target object's value.

Transforming Assigned Values:

  • JavaScript

JavaScript

var target = { 'x': 10, 'y': 20 };

var source = { 'x': 5, 'z': 15 };

_.extendWith(target, source, (objVal, srcVal) => {

 return objVal * srcVal;

});

console.log(target);
You can also try this code with Online Javascript Compiler
Run Code

Output: 

{ 'x': 50, 'y': 20, 'z': 15 }


An example of transforming values during assignment by multiplying them.

Merging Objects with Priority:

  • JavaScript

JavaScript

var base = { 'name': 'John', 'age': 30 };

var changes = { 'name': 'Jane', 'age': 25 };

_.extendWith(base, changes, (baseVal, changeVal) => baseVal);

console.log(base);
You can also try this code with Online Javascript Compiler
Run Code

Output: 

{ 'name': 'John', 'age': 30 }


Demonstrates prioritizing the base object's properties over changes.

Frequently Asked Questions

How does _.extendWith() differ from _.assignWith()?

While _.assignWith() only copies an object's own properties, _.extendWith() (or _.assignInWith()) copies both own and inherited properties from the prototype chain.

Is _.extendWith() suitable for deep merging of objects?

_.extendWith() performs a shallow merge. For deep merging, a method like _.mergeWith() should be used.

What happens in case of property name conflicts?

If multiple source objects have properties with the same name, _.extendWith() will overwrite the property in the target object with the last source's value for that property, unless the customizer function specifies otherwise.

Conclusion

Lodash's _.extendWith() method is a powerful tool for merging properties from source objects into a target object, including inherited properties, with the added flexibility of a customizer function. It's particularly valuable in complex scenarios where standard object merging is not sufficient and specific rules need to be applied for property assignment.

You can refer to our guided paths on the Coding Ninjas. You can check our course to learn more about DSADBMSCompetitive ProgrammingPythonJavaJavaScript, etc. 

Also, check out some of the Guided Paths on topics such as Data Structure and AlgorithmsCompetitive ProgrammingOperating SystemsComputer Networks, DBMSSystem Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.

Live masterclass