
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
How to merge properties of two JavaScript Objects dynamically?
There are two methods to merge properties of javascript objects dynamically. They are
1) Object.assign()
The Object.assign() method is used to copy the values of all properties from one or more source objects to a target object. It will return the target object.
Example-1
<html> <body> <script> var target = { a: "ram", b: "rahim" }; var source = { c: "akbar", d: "anthony" }; var returnedTarget = Object.assign(target, source); document.write(JSON.stringify(target)); document.write(JSON.stringify(returnedTarget)); </script> </body> </html>
output
{"a":"ram","b":"rahim","c":"akbar","d":"anthony"} {"a":"ram","b":"rahim","c":"akbar","d":"anthony"}
If the objects have the same keys, then the value of the key of the object which appears later in the distribution will be copied. The following example shows up the scenario when there are same keys with different values.
Example-2
<html> <body> <script> var target = { a: "ram", b: "rahim" }; var source = { b: "akbar", d: "anthony" }; var returnedTarget = Object.assign(target, source); document.write(JSON.stringify(target)); document.write("</br>"); document.write(JSON.stringify(returnedTarget)); </script> </body> </html>
Output
{"a":"ram","b":"akbar","d":"anthony"} {"a":"ram","b":"akbar","d":"anthony"}
2) Using spread operator
The spread operator allows an expression to be expanded in places where multiple elements/variables/arguments are expected. It is mostly used in a variable array where there is multiple values are expected. Since javascript objects are key value paired entities, we can merge them in to one using spread operator.
syntax
var target = [...obj1, ...obj2, ...]
Example
<html> <body> <script> var target = { a: "ram", b: "rahim" }; var source = { b: "akbar", d: "anthony" }; var returnedTarget = {...target, ...source} document.write(JSON.stringify(returnedTarget)); </script> </body> </html>
Output
{"a":"ram","b":"akbar","d":"anthony"}
- Related Articles
- How can I merge properties of two JavaScript objects dynamically?
- How to merge two JavaScript objects?
- How to merge two different array of objects using JavaScript?
- How to merge two arrays with objects in one in JavaScript?
- JavaScript Sum of two objects with same properties
- Merge two objects in JavaScript ignoring undefined values
- Sort Array of objects by two properties in JavaScript
- How to access properties of an array of objects in JavaScript?
- How to merge two arrays in JavaScript?
- How can we merge two JSON objects in Java?
- Merge and group object properties in JavaScript
- How to merge objects into a single object array with JavaScript?
- How to merge two strings alternatively in JavaScript
- JavaScript Separate objects based on properties
- How to compare two JavaScript Date Objects?
