How to copy properties from one object to another in ES6 ?
Last Updated :
26 Nov, 2021
The properties of one object can be copied into another object through different methods. Here are some of those methods.
Object.assign(): By using the Object.assign() method, all enumerable properties of one or more source objects are copied to the target object. This method returns the modified target object. The properties of the target object are overwritten if they have the same key as properties in the sources. Properties from later sources overwrite properties from earlier sources. Object.assign() only copies enumerable and own properties from one object to another. As a result, it invokes getters and setters on the source and target. As a result, it assigns properties rather than copies or defines new ones. The merge sources may contain getters, thus making it unsuitable for merging new properties into prototypes.
Syntax:
Object.assign(target, ...sources)
Example:
JavaScript
<script>
let obj1 = { a: 1, b: 2 };
let obj2 = { a: 3, c: 4 };
let obj3 = { b: 5, d: 6 };
// Target object itself is changed.
let targetobj1 = Object.assign(obj1, obj2);
// Here the obj1 and targetobj1 both are same
console.log(obj1);
console.log(targetobj1);
// Passing multiple objects and copy
// to the target obj
let targetobj2 = Object.assign(obj1, obj2, obj3);
console.log(obj1);
console.log(targetobj2);
</script>
Output:
{
a: 3,
b: 2,
c: 4
}
{
a: 3,
b: 2,
c: 4
}
{
a: 3,
b: 5,
c: 4,
d: 6
}
{
a: 3,
b: 5,
c: 4,
d: 6
}
Using Spread Operator: An array expression or string may have spread syntax (...) to expand where there are no or multiple arguments (for function calls) or elements (for array literals), or an object expression may be expanded where there are no or multiple key-value pairs (for object literals).
JavaScript
<script>
let obj1 = { a: 1, b: 2 };
let obj2 = { a: 3, c: 4 };
let obj3 = { b: 5, d: 6 };
// (...) is syntax of spread operator
let targetobj = { ...obj1, ...obj2, ...obj3 };
console.log(targetobj);
</script>
Output:
{
a: 3,
b: 5,
c: 4,
d: 6
}
By using for loop: It is necessary to iterate over the property of the source object (i.e. obj2) and assign the property with the value to target object obj1. But this method is limited to two objects if we use one for loop.
JavaScript
<script>
let obj1 = { a: 1, b: 2 };
let obj2 = { a: 3, c: 4 };
for (let key in obj2) {
obj1[key] = obj2[key];
}
console.log(obj1);
</script>
Output:
{
a: 3,
b: 2,
c: 4
}
Similar Reads
How to filter an array of objects in ES6 ? In this article, we will try to understand how we could filter out or separate certain data from the array of objects in ES6.Let us first try to understand how we could create an array of objects by following certain syntax provided by JavaScript.Table of ContentJavascript Array of ObjectsJavascript
4 min read
How to add and remove properties from objects in JavaScript ? We will try to understand how to add properties to an object and how to add or remove properties from an object in JavaScript.Before we go and see the addition and removal of properties from an object let us first understand the basics of an object in JavaScript.Object:An object in JavaScript is a c
6 min read
How to read properties of an Object in JavaScript ? Objects in JavaScript, it is the most important data type and forms the building blocks for modern JavaScript. These objects are quite different from JavaScriptâs primitive data-types(Number, String, Boolean, null, undefined, and symbol) in the sense that these primitive data-types all store a singl
2 min read
How to get a subset of a javascript object's properties? To get the subset of properties of a JavaScript Object, we make use of destructuring and Property Shorthand. The destructuring assignment syntax is a JavaScript expression that makes it possible to unpack values from arrays, or properties from objects, into distinct variables.Syntax: subset = (({a,
2 min read
How to Remove a Property From JavaScript Object? The delete operator is used to remove a property from a JavaScript object. The delete operator allows you to remove a specified property from an object. Once a property is deleted, it no longer exists in the object.Using delete OperatorThe basic method to remove a property from a JavaScript object i
3 min read
How to create object properties in JavaScript ? JavaScript is built on an object-oriented framework. An object is a collection of properties, where each property links a key to a value. These properties are not in any specific order. The value of a JavaScript property can be a method (function). Object properties can be updated, modified, added,
4 min read