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.
Basic Object Merging:
4.2.
JavaScript
4.3.
Merging Multiple Objects:
4.4.
JavaScript
4.5.
Updating Object Properties:
4.6.
JavaScript
4.7.
Extending Object with Additional Properties:
4.8.
JavaScript
5.
Frequently Asked Questions
5.1.
How does _.assign() differ from Object.assign() in JavaScript?
5.2.
Does _.assign() perform deep cloning?
5.3.
What happens if there are property conflicts?
6.
Conclusion
Last Updated: Mar 27, 2024
Easy

Lodash _.assign() Method

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

Introduction

In JavaScript, merging objects is a common task, especially when dealing with configurations, state objects, or complex data structures. Lodash simplifies this process with the _.assign() method. This method copies the own enumerable string keyed properties of source objects to a target object. 

Lodash _.assign() Method

It's particularly useful when you need to combine properties from multiple objects into a single object, often used in settings where object composition is required.

Why This Function is Used

The _.assign() function is used for object assignment. It assigns properties from one or more source objects to a target object. This method is essential when you want to compose a new object by combining properties from existing objects, especially useful in scenarios like extending settings, updating state in state management patterns, or merging configuration objects.

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

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

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

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

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