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 Content
Using Dot Notation
In this approach, we will directly access the key of an object by "." for assigning the value to it. It can be a repeated method as every time we need to define a different key for different values.
Example: This example shows the implementation of the above approach.
const obj = { Organisation: "GFG" };
obj.field = "CS";
console.log(obj);
Output:
{ Organisation: 'GFG', field: 'CS' }
Using Bracket Notation
In this approach, we are using Bracket to access the key and assign the value to it.
Example: This example shows the implementation of the above approach.
const obj = { Organisation: "GFG" };
const Field = "field";
const Year = "year";
const lang = "lang";
obj[Field] = "CS";
obj[Year] = "2023";
obj[lang] = "English";
console.log(obj);
Output:
{ Organisation: 'GFG', field: 'CS', year: '2023', lang: 'English' }
Using Object.assign() Method
In this approach, we are using Object.assign() method to assign the key and value to the object. We are passing the object name and the field that we want to use in the object. It assign those fields to the given object.
Example: This example shows the implementation of the above approach.
const obj = { Organisation: "GFG" };
Object.assign(obj, { field: "CS" });
console.log(obj);
Output:
{ Organisation: 'GFG', field: 'CS' }
Using Variable as a Key
If your key name is inside a variable, you must use bracket notation.
let obj = {};
let keyName = "city";
obj[keyName] = "New York";
console.log(obj);
Output:
{ city: 'New York' }
Using Object.defineProperty() Method
In this approach, we are using Object.defineProperty() method to assign the key and value to the object. We are passing the object name and the field that we want to use in the object with some extra specification that is served by the method itself.
Example: This example shows the implementation of the above approach.
const obj = { Organisation: "GFG" };
Object.defineProperty(obj, "field", {
value: "CS",
writable: false,
enumerable: true,
configurable: true,
});
console.log(obj);
// This won't change the 'field' key because
// 'writable' is set to false
obj.afield = "Civil";
console.log(obj);
Output:
{ Organisation: 'GFG', field: 'CS' }
{ Organisation: 'GFG', field: 'CS', afield: 'Civil' }
Using Object.entries() and Object.fromEntries()
In this approach, we are using Object.entries() to convert the object into an array of key-value pairs, then adding the new key-value pair, and converting it back to an object using Object.fromEntries().
Example: This example shows the implementation of the above approach.
const obj = { Organisation: "GFG" };
const entries = Object.entries(obj);
entries.push(['field', 'CS']);
const newObj = Object.fromEntries(entries);
console.log(newObj);
{ Organisation :'GFG' , field:'CS' }
Using Spread Operator
The spread operator is used to copy all properties from one object into another, or merge objects easily.
<!DOCTYPE html>
<html>
<head>
<title>Spread Operator Example</title>
</head>
<body>
<h2>Check console for output</h2>
<script>
// Original object
const person = {
name: "Alice",
age: 25
};
// Add a new key-value pair using spread operator
const updatedPerson = {
...person,
city: "New York",
profession: "Engineer"
};
console.log("Original Object:", person);
console.log("Updated Object:", updatedPerson);
</script>
</body>
</html>
Output:
Updated Object: { name: 'Alice', age: 25, city: 'New York', profession: 'Engineer' }
Using Map (alternative to Plain Objects)
A Map
allows keys of any type and maintains insertion order. It is used when you need more flexibility.
// Create a new Map
const person = new Map();
// Add key-value pairs
person.set('name', 'Grace');
person.set('age', 27);
person.set('city', 'Berlin');
console.log(person);
// Get a value
console.log(person.get('name'));
Output:
Map(3) { 'name' => 'Grace', 'age' => 27, 'city' => 'Berlin' }
Grace