How to usethe for-of loop in Javascript

Using a for loop to traverse an array of objects, finding and modifying the property of a specific object as per a condition.

Syntax:

for ( variable of iterableObjectName) {
...
}

Example: In this example, we will try to use the same array of objects which we have created previously, and then we will use the for-of loop in order to update the existing object’s property’s value.

Javascript
let employees_data = [
    {
        employee_id: 1,
        employee_name: "Aman",
    },
    {
        employee_id: 2,
        employee_name: "Bhargava",
    },
    {
        employee_id: 3,
        employee_name: "Chaitanya",
    },
];
for (let object of employees_data) {
    if (object.employee_id === 2) {
        object.employee_name = "Anthony";
    }
}
console.log("Updated Data: ");
console.log(employees_data);

Output
Updated Data: 
[
  { employee_id: 1, employee_name: 'Aman' },
  { employee_id: 2, employee_name: 'Anthony' },
  { employee_id: 3, employee_name: 'Chaitanya' }
]

How to modify an object’s property in an array of objects in JavaScript ?

Modifying an object’s property in an array of objects in JavaScript involves accessing the specific object within the array and updating its property. In this article we are going to see how to modify an object’s property in an array of objects in JavaScript

Below are the approaches to modify an object’s property in an array of objects in JavaScript:

Table of Content

  • Approach 1: Using the Array.map() method
  • Approach 2: Using the for-of loop
  • Approach 3: Using Array.map() with spread operator
  • Approach 4: Using forEach() method
  • Approach 5: Using the find() method and destructuring
  • Approach 6: Using Array.reduce() Method
  • Approach 7: Using Array.filter() and Array.concat() Methods

Similar Reads

Approach 1: Using the Array.map() method

Using the map() method to create a new array by transforming each element of the original array based on a specified function....

Approach 2: Using the for-of loop

Using a for loop to traverse an array of objects, finding and modifying the property of a specific object as per a condition....

Approach 3: Using Array.map() with spread operator

Using Array.map() to create a new array with the spread operator to modify specific object properties....

Approach 4: Using forEach() method

Using forEach() method, iterate through the array of objects, check the condition, and modify the property of the matching object....

Approach 5: Using the find() method and destructuring

Using find() to search for the object with the specified property value, and destructuring to modify the property....

Approach 6: Using Array.reduce() Method

The Javascript arr.reduce() method in JavaScript is used to reduce the array to a single value and executes a provided function for each value of the array (from left to right) and the return value of the function is stored in an accumulator....

Approach 7: Using Array.filter() and Array.concat() Methods

Using Array.filter() to create a new array excluding the object that needs modification and then using Array.concat() to add the modified object back to the array....