How to Append an Object as a Key Value in an Existing Object in JavaScript ?
Last Updated :
09 Jun, 2024
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.
Using JavaScript Spread (...) Operator
The Spread (...) Operator in JavaScript allows to copy all the elements in the existing object into a new object.
Example: Demonstrating appending new object to existing object as a key using the spread operator.
JavaScript
let Object1 = {
Name: "Poojitha",
Age: 20,
};
let Object2 = {
Occupation: "Content Writer",
};
Object1 = {
...Object1,
...Object2,
};
console.log(Object1);
Output{ Name: 'Poojitha', Age: 20, Occupation: 'Content Writer' }
Using JavaScript Object.assign() method
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.
JavaScript
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' }
Using JavaScript Bracket notation ([])
The JavaScript bracket notation helps to directly assign the new object as a key-value pair to the existing object.
Example: We will create a new key that is similar to the key in the object2 and assign the value to it taken from the object1 by using bracket notation.
JavaScript
let Object1 = {
Name: "Poojitha",
Age: 20,
};
let Object2 = {
Occupation: "Content Writer",
};
Object1["Occupation"] = Object2["Occupation"];
console.log(Object1);
Output{ Name: 'Poojitha', Age: 20, Occupation: 'Content Writer' }
Using JavaScript object.defineProperty() method
The Object.defineProperty() method helps to define a new property in the specified object. To make the newly created property enumerable and writable, we set the `enumerable` and `writable` properties to `true`.
Syntax:
Object.defineProperty(object, property_name, descriptor)
Example: To demonstrate appending new object to the existing object as a key using the Object.defineProperty() method.
JavaScript
let Object1 = {
Name: "Poojitha",
Age: 20,
};
let Object2 = {
Occupation: "Content Writer",
};
Object.defineProperty(Object1, "Occupation", {
value: Object2["Occupation"],
enumerable: true,
writable: true,
});
console.log(Object1);
Output{ Name: 'Poojitha', Age: 20, Occupation: 'Content Writer' }
Using Object.entries() and Array.prototype.forEach()
In this approach, we use the Object.entries() method to get an array of key-value pairs from the new object. Then, we use the Array.prototype.forEach() method to iterate over these pairs and add them to the existing object.
Syntax:
Object.entries(newObject).forEach(([key, value]) => {
existingObject[key] = value;
});
Example: This example demonstrates how to use Object.entries() and Array.prototype.forEach() to append a new object to an existing object.
JavaScript
let Object1 = {
Name: "Nikunj",
Age: 22,
};
let Object2 = {
Occupation: "Content Writer",
};
Object.entries(Object2).forEach(([key, value]) => {
Object1[key] = value;
});
console.log(Object1);
Output{ Name: 'Nikunj', Age: 22, Occupation: 'Content Writer' }
Similar Reads
How to get a key in a JavaScript object by its value ? To get a key in a JavaScript object by its value means finding the key associated with a specific value in an object. Given an object with key-value pairs, you want to identify which key corresponds to a particular value, often for searching or data retrieval.How to get a key in a JavaScript object
4 min read
How to Add Duplicate Object Key with Different Value to Another Object in an Array in JavaScript ? Adding duplicate object keys with different values to another object in an array in JavaScript refers to aggregating values under the same key from multiple objects in an array, creating a new object where each key corresponds to an array of associated values. Table of Content Using for...of LoopUsi
4 min read
How Check if object value exists not add a new object to array using JavaScript ? In this tutorial, we need to check whether an object exists within a JavaScript array of objects and if they are not present then we need to add a new object to the array. We can say, every variable is an object, in Javascript. For example, if we have an array of objects like the following. obj = {
3 min read
How to Add an Image File to an Object of an Array in JavaScript ? In JavaScript adding the image files into objects within an array is essential for managing and organizing data in web development. This practice is particularly valuable when dealing with collections of objects, each requiring an associated image. There are various ways to add an image file to an o
3 min read
How to Create Array of Objects From Keys & Values of Another Object in JavaScript ? Creating an array of objects from the keys and values of another object involves transforming the key-value pairs of the original object into individual objects within an array. Each object in the array represents a key-value pair from the source object. Below approaches can be used to accomplish th
3 min read
How to Convert an Object into Array of Objects in JavaScript? Here are the different methods to convert an object into an array of objects in JavaScript1. Using Object.values() methodObject.values() method extracts the property values of an object and returns them as an array, converting the original object into an array of objects.JavaScriptconst a = { java:
3 min read
How to store a key=> value array in JavaScript ? In JavaScript, storing a key-value array means creating an object where each key is associated with a specific value. This allows you to store and retrieve data using named keys instead of numeric indices, enabling more intuitive access to the stored information.Here are some common approaches:Table
4 min read
How to invert key value in JavaScript object ? JavaScript is a high-level, interpreted, and dynamically typed client-side scripting language. JavaScript is used to add dynamic features to the static HTML. Everything is an object in JavaScript. Objects in JavaScript can be declared using figure brackets {..} and the objects may comprise certain p
2 min read
How to add Key-Value pair to a JavaScript Object? A JavaScript object has a key-value pair, and it can be of variable length. We first need to declare an object and assign the values to that object for that there can be many methods.Below are the methods to add a key/value pair to a JavaScript object:Table of ContentUsing Dot NotationUsing Bracket
4 min read
How to Store an Object Inside an Array in JavaScript ? Storing an object inside an array in JavScript involves placing the object as an element within the array. The array becomes a collection of objects, allowing for convenient organization and manipulation of multiple data structures in a single container. Following are the approaches through which it
3 min read