Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
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 _.assignWith() differ from _.assign()?
5.2.
What happens if the customizer returns undefined?
5.3.
Can _.assignWith() be used to deep merge objects?
6.
Conclusion
Last Updated: Mar 27, 2024
Easy

Lodash _.assignWith() Method

Author Riya Singh
0 upvote
Create a resume that lands you SDE interviews at MAANG
Speaker
Anubhav Sinha
SDE-2 @
12 Jun, 2024 @ 01:30 PM

Introduction

Merging objects in JavaScript, while straightforward with basic properties, can become complex when you need more control over how values are assigned and merged. Lodash's _.assignWith() method enhances the functionality of _.assign() by allowing a custom function to determine how assigned values are merged.

Lodash _.assignWith() Method

This method is particularly useful in scenarios where standard object merging doesn't suffice, and specific rules need to be applied to resolve conflicts or transform values during the assignment.

Why This Function is Used

The _.assignWith() function is used when you need to assign properties from one or more source objects to a target object, with a custom rule for how these properties are assigned. This method is essential in situations where you want to customize the assignment behavior, such as handling conflicts between properties or applying specific transformations to the values during the assignment process.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Syntax, Parameter and Return Value

Syntax: 

_.assignWith(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] };

_.assignWith(object, other, customizer);

console.log(object);

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' };

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

 return objVal ? objVal : srcVal;

});

console.log(target);

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 };

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

 return objVal * srcVal;

});

console.log(target);

 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 };

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

console.log(base);

Output: 

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


Demonstrates prioritizing the base object's properties over changes.

Frequently Asked Questions

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

While _.assign() copies properties from source objects to the target object, _.assignWith() allows for a custom function to dictate how these values are assigned or merged.

What happens if the customizer returns undefined?

If the customizer function returns undefined, property assignments are handled by the method's default behavior.

Can _.assignWith() be used to deep merge objects?

_.assignWith() performs a shallow merge. For deep merging, a custom implementation or a different method like _.mergeWith() would be required.

Conclusion

Lodash's _.assignWith() method offers an advanced way to merge properties from source objects into a target object, with the 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.

Previous article
Appendchild Javascript
Next article
Lodash _.negate() Method
Live masterclass