Syntax, Parameter and Return Value
Syntax:
_.assignInWith(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
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] };
_.assignInWith(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
var target = { 'prop1': 'value1' };
var source = { 'prop1': 'value2', 'prop2': 'value3' };
_.assignInWith(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
var target = { 'x': 10, 'y': 20 };
var source = { 'x': 5, 'z': 15 };
_.assignInWith(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
var base = { 'name': 'John', 'age': 30 };
var changes = { 'name': 'Jane', 'age': 25 };
_.assignInWith(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 _.assignInWith() differ from _.assignWith()?
_.assignInWith() includes inherited properties in the assignment, while _.assignWith() only considers the object's own properties.
What happens if the customizer returns undefined?
If the customizer function returns undefined, property assignments are handled by the method's default behavior.
Is it safe to mutate the target object in the customizer function?
While you can mutate the target object, it's generally advisable to avoid side effects in the customizer to maintain predictable behavior.
Conclusion
Lodash's _.assignInWith() method offers an advanced way to merge properties from source objects into a target object, with the added flexibility of a customizer function. It's particularly valuable in complex object manipulation scenarios where control over how properties are merged or overridden is required.
You can refer to our guided paths on the Coding Ninjas. You can check our course to learn more about DSA, DBMS, Competitive Programming, Python, Java, JavaScript, etc.
Also, check out some of the Guided Paths on topics such as Data Structure and Algorithms, Competitive Programming, Operating Systems, Computer Networks, DBMS, System Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.