JavaScript - Modify Object's Property in an Array of Objects
Last Updated :
28 Jun, 2025
To modify an object’s property in an array of objects, you can use methods like forEach(), map(), or find() to locate the object and then modify its properties.
Here are some methods to modify an object's property in an array of objects:
1. Using the Array.map() method
The map() method returns a new array with modified objects. It is useful when you want to create a new array based on modifications.
JavaScript
let a = [
{ name: "Sourav", age: 23 },
{ name: "Ajay", age: 25 }
];
let updatedArr = a.map(item => {
if (item.name === "Ajay") {
item.age = 26;
}
return item;
});
console.log(updatedArr);
Output[ { name: 'Sourav', age: 23 }, { name: 'Ajay', age: 26 } ]
In this example
The map() method is used to create a new array (updatedArr) where we modify the age of the object with the name "Ajay".
- The original array remains unchanged.
2. Using for loop
The for loop is a basic and versatile method to iterate through an array and modify its elements.
JavaScript
let a = [
{ name: "Sourav", age: 23 },
{ name: "Ajay", age: 25 }
];
for (let i = 0; i < a.length; i++) {
if (a[i].name === "Ajay") {
a[i].age = 26;
}
}
console.log(a);
Output[
{ id: 1, name: 'Alia', age: 30 },
{ id: 2, name: 'Bobby', age: 26 },
{ id: 3, name: 'Charu', age: 35 }
]
In this example
- A simple for loop is used to iterate through the array.
- When the object with the name "Ajay" is found, its age property is updated to 26.
3. Using forEach() method
The forEach() method in JavaScript is used to iterate over an array and execute a provided function on each element. It is a simple and intuitive way to modify a property of an object within an array.
JavaScript
let a = [
{ name: "Sourav", age: 23 },
{ name: "Ajay", age: 25 }
];
a.forEach(item => {
if (item.name === "Sourav") {
item.age = 24;
}
});
console.log(a);
Output[ { name: 'Sourav', age: 24 }, { name: 'Ajay', age: 25 } ]
In this example
- The forEach() method iterates over each object in the array.
- When the object with the name "Sourav" is found, its age property is updated to 24.
4. Using the find() method and destructuring
If you only need to modify the first object that matches a condition, you can use find() to get the object and modify its properties.
JavaScript
let a = [
{ name: "Sourav", age: 23 },
{ name: "Ajay", age: 25 }
];
let obj = a.find(item => item.name === "Sourav");
if (obj) {
obj.age = 24;
}
console.log(a);
Output[ { name: 'Sourav', age: 24 }, { name: 'Ajay', age: 25 } ]
In this example
- The find() method returns the first object that matches the condition (where name is "Sourav").
- After finding the object, we modify its age property directly.
5. Using Array.reduce() Method
The reduce() method can be used to modify an object in the array while building a new array.
JavaScript
let a = [
{ name: "Sourav", age: 23 },
{ name: "Ajay", age: 25 }
];
let updatedArr = a.reduce((acc, item) => {
if (item.name === "Sourav") {
item.age = 24;
}
acc.push(item);
return acc;
}, []);
console.log(updatedArr);
Output[ { name: 'Sourav', age: 24 }, { name: 'Ajay', age: 25 } ]
In this example
- The reduce() method iterates over the array and modifies the object that matches the condition.
- The modified object is added to a new accumulator array.
6. Using findIndex() Method
The findIndex() method finds the index of the first object that satisfies the condition, and you can use this index to modify the object directly in the array.
JavaScript
let a = [
{ name: "Sourav", age: 23 },
{ name: "Ajay", age: 25 }
];
let index = a.findIndex(item => item.name === "Sourav");
if (index !== -1) {
a[index].age = 24;
}
console.log(a);
Output[ { name: 'Sourav', age: 24 }, { name: 'Ajay', age: 25 } ]
In this example
- The findIndex() method returns the index of the first object that matches the condition.
- Once the index is found, we can directly modify the object at that index.
Similar Reads
How to get dynamic access to an object property in JavaScript ? In JavaScript, an object is a collection of properties, where each property consists of a key-value pair. Objects are a fundamental data type in JavaScript. You can create an object in JavaScript in the following ways: By using the object literal notation, which is a comma-separated list of key-valu
7 min read
How to Remove an Entry by Key in JavaScript Object? In JavaScript, objects store data in the form of key-value pairs where the key may be any property of the object. In this article let us see how to remove key-value pairs a by given key in the object.Table of ContentUsing the delete operatorUsing the filter() methodUsing Destructuring and Object.ass
3 min read
JavaScript - Modify Object's Property in an Array of Objects To modify an objectâs property in an array of objects, you can use methods like forEach(), map(), or find() to locate the object and then modify its properties. Here are some methods to modify an object's property in an array of objects:1. Using the Array.map() methodThe map() method returns a new a
4 min read
How to compare Arrays of Objects in JavaScript? In JavaScript, comparing arrays of objects can be more complex than comparing primitive data types. We will discuss different ways to compare arrays of objects effectively, with detailed code examples and explanations.Syntax: Before going to detail the comparison techniques, let's first understand h
5 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
How to create an object with prototype in JavaScript ? In this article, we will discuss object creation & prototypes, along with understanding the different ways for object creation & their implementation through the examples. Prototypes are the mechanism by which objects in JavaScript inherit features from another object. A prototype property i
4 min read
How to declare object with computed property name in JavaScript ? In this article, we learn how to declare an object with a computed property name. Before beginning this article, we have to know about the javascript objects. Computed Property Names: The ES6 computed property names feature allows us to compute an expression as a property name on an object. Javascri
2 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 check if a value is object-like in JavaScript ? In JavaScript, objects are a collection of related data. It is also a container for name-value pairs. In JavaScript, we can check the type of value in many ways. Basically, we check if a value is object-like using typeof, instanceof, constructor, and Object.prototype.toString.call(k). All of the ope
4 min read
JavaScript - Convert Two-Dimensional Array Into an Object Here are the different methods to convert the two-dimensional array into an object in JavaScript.1. Using a for LoopThe simplest way to convert a two-dimensional array into an object is by iterating through the array using a for loop and assigning key-value pairs.JavaScriptconst a = [['name', 'Abhay
2 min read