Syntax, Parameter and Return Value
Syntax:
_.assign(target, ...sources)
Parameters:
-
target (Object): The destination object.
- ...sources (Object): The source objects.
Return Value:
(Object) - Returns the modified target object.
Examples
Basic Object Merging:
JavaScript
var _ = require('lodash');
var object = { 'a': 1 };
var other = { 'b': 2 };
_.assign(object, other);
console.log(object);

You can also try this code with Online Javascript Compiler
Run Code
Output:
{ 'a': 1, 'b': 2 }
Demonstrates merging two objects into one.
Merging Multiple Objects:
JavaScript
var defaults = { a: 1, b: 2 };
var config = { b: 3, c: 4 };
var options = { c: 5 };
var result = _.assign({}, defaults, config, options);
console.log(result);

You can also try this code with Online Javascript Compiler
Run Code
Output:
{ a: 1, b: 3, c: 5 }
Shows how to merge multiple objects, combining and overriding properties.
Updating Object Properties:
JavaScript
var state = { loading: false, data: null };
var newState = _.assign({}, state, { loading: true, data: { items: [] }});
console.log(newState);

You can also try this code with Online Javascript Compiler
Run Code
Output:
{ loading: true, data: { items: [] }}
An example of using _.assign() to update properties in a state object.
Extending Object with Additional Properties:
JavaScript
var user = { name: 'John Doe' };
var additionalInfo = { age: 30, occupation: 'Developer' };
_.assign(user, additionalInfo);
console.log(user);

You can also try this code with Online Javascript Compiler
Run Code
Output:
{ name: 'John Doe', age: 30, occupation: 'Developer' }
Demonstrates extending an object with additional properties from another object.
Frequently Asked Questions
How does _.assign() differ from Object.assign() in JavaScript?
_.assign() and Object.assign() perform similarly in that they copy properties from source objects to a target object. However, _.assign() works well with Lodash's utility-first approach and chaining capabilities.
Does _.assign() perform deep cloning?
No, _.assign() performs a shallow copy of properties. It does not clone nested objects but rather copies their references.
What happens if there are property conflicts?
If source objects have the same property, _.assign() will overwrite the property in the target object with the last source's value for that property.
Conclusion
Lodash's _.assign() method is a powerful tool for merging properties from one or more source objects into a target object. It is especially useful for object composition, updating states, or extending configurations in a clear and readable manner.
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.