How to use Object.assign() method In Javascript
The Object.assign() method is used to copy the data from the specified enumerable objects to the target object.
Syntax:
Object.assign(target_object, source_object)
Example: Appending new object to existing object as a key using assign method.
let Object1 = {
Name: "Poojitha",
Age: 20,
};
let Object2 = {
Occupation: "Content Writer",
};
Object.assign(Object1, Object2);
console.log(Object1);
Output
{ Name: 'Poojitha', Age: 20, Occupation: 'Content Writer' }
How to Append an Object as a Key Value in an Existing Object in JavaScript ?
In JavaScript, An object is a key-value pair structure. The key represents the property of the object and the value represents the associated value of the property. In JavaScript objects, we can also append a new object as a Key-value pair in an existing object in various ways which are as follows.
Table of Content
- Using JavaScript Spread (…) Operator
- Using JavaScript Object.assign() method
- Using JavaScript Bracket notation ([])
- Using JavaScript object.defineProperty() method
Contact Us