
- 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 duplicate Javascript object properties in another object?
In JavaScript, objects are the collection of a key value pairs. The properties of the object are the keys and is denoted with a string. The value of the key is the value of the property of the given object.
In JavaScript, the objects can be copied to other by many ways in which some of them are −
Using spread operator(…)
The spread operator in JavaScript is used to copy the values of the original given object to a new object. This operator is represented by three dots(…).
Example 1
This example demonstrates how spread operator is used to copy objects in JavaScript −
var employee = { emp_name: "Abdul Rawoof", company: "Tutorials Point", salary: 18000, job: "Software Engineer-Intern" } console.log("The original object employee is:", employee) var cpyEmployee = {...employee} console.log("The copied object cpyEmployee is:",cpyEmployee);
If the object has nested object, then some additional lines should be written when spread operator is used.
Using assign() function
This function is used to copy the original object into a new object. The difference in this and the spread operator is when nested objects are present and if assign() is used to copy the object, then the nested object will not change and the other variables of the object can be changed.
There are two parameters to the assign() function. The first parameter is curly braces {} which is used to ensure that the new object will not mutate the original object. The second parameter is the name of the original object which is to be copied into new object.
Syntax
Object.assign({},originalObjectName)
Example 1
Following is an example of the assign() method. In here we are creating an object and copying its contents into a new object −
var employee = { emp_name: "Abdul Rawoof", company: "Tutorials Point", salary: 18000, job: "Software Engineer-Intern" } console.log("The original object employee is:", employee) var cpyEmployee = Object.assign({},employee) console.log("The copied object cpyEmployee is:",cpyEmployee); cpyEmployee.emp_name="Jason" cpyEmployee.job="Content writer-Intern" console.log("The copied object with some different name and role is:",cpyEmployee)
In the above example the original object is copied into the new object and then the properties of the objects are changed. So, from this it is concluded that we can update the values of the properties of the object after copying it from the original object.
When these are copied by using the assign() function the original value and the new copied value will have the same reference. So, change in one can alter the change in the other accordingly.
Example 2
Following is an another example of the assign() method. To copy all the properties of a source object onto a target object, you can use the following code −
const targetObj = { a: 1, b: 2 }; const sourceObj = { b: 4, c: 5 }; const returnedTarget = Object.assign(targetObj, sourceObj); console.log(targetObj); console.log(returnedTarget); console.log(returnedTarget === targetObj); console.log(sourceObj);
- Related Articles
- How to add properties from one object into another without overwriting in JavaScript?
- How to create object properties in JavaScript?
- How to delete object properties in JavaScript?
- How to access an object through another object in JavaScript?
- JavaScript Object Properties
- How to add, access, delete, JavaScript object properties?
- How to modify properties of a nested object in JavaScript?
- How to add properties and methods to an object in JavaScript?
- How to create an object and access its properties in JavaScript?
- Extract properties from an object in JavaScript
- Merge and group object properties in JavaScript
- How to add properties and methods to an existing object in JavaScript?
- How to iterate a JavaScript object's properties using jQuery?
- How to get a subset of JavaScript object's properties?
- List all duplicate values in a nested JavaScript object
