Open In App

How to use forEach with an Array of Objects in JavaScript ?

Last Updated : 23 Jan, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

In JavaScript, forEach is used to iterate over an array of objects or an array to perform operations on the array of objects. The forEach method takes a callback function as an argument and this function is executed once for each element of the array, The callback function takes two or three parameters where the first parameter is the current element and the second parameter is the index of the current element. Each iteration of the forEach loop will provide you with access to each element of the object.

Syntax:

array.forEach( function( currentValue, index, array);

Parameters:

  • Callback function: This function is called for each element of the array.
  • Current Value: This holds the value that is being processed currently.
  • Index: It holds the index of the current value of the array element starting from 0( This is an optional parameter).
  • array: It holds the complete array on which the method is called ( This is also an optional parameter).

Example 1: Iterating over the array of objects using forEach.

Javascript




let arrayOfObject = [
    { name: "GeeksforGeeks", location: "Noida" },
    {
        courses: [
            "DSA",
            "DevOps Bootcamp",
            "GATE preparation course etc...."
        ]
    },
];
 
arrayOfObject.forEach((obj) => {
    if (obj.name) {
        console.log(`${obj.name} is
         located in ${obj.location}`);
    } else if (obj.courses) {
        console.log(`Courses offered:
   ${obj.courses.join(", ")}`);
    }
});


Output

GeeksforGeeks is
         located in Noida
Courses offered:
   DSA, DevOps Bootcamp, GATE preparation course etc....

Example 2: Using forEach to iterate over the array of Employee details.

Javascript




const Employees = [
    {
        name: 'John', age: 35,
        team: "Data Strcuture & Alogirthm"
    },
    {
        name: 'Doe', age: 26,
        team: "DevOps Bootcamp"
    },
    {
        name: 'Ben', age: 21,
        team: "Data Science"
    }
];
 
// Using forEach loop.
Employees.forEach((Employees) => {
    console.log(
        `
  ${Employees.name}is ${Employees.age} years old
  and is the part of ${Employees.team} team.\n
        `
    );
});


Output

  Johnis 35 years old
  and is the part of Data Strcuture & Alogirthm team.

        

  Doeis 26 years old
  and is the part of DevOps Bootcamp team.

        

  Benis 21 years old
  and is the par...

Example 3: Find the area of the rectangle using forEach by iterating over the dimensions array.

Javascript




let dimensions = [
    {
        rectangle1: {
            length: 10,
            width: 5
        }
    },
    {
        rectangle2: {
            length: 20,
            width: 10
        }
    },
    {
        rectangle3: {
            length: 30,
            width: 15
        }
    }
];
 
dimensions.forEach(rectangleObj => {
    const rectangle = Object.keys(rectangleObj)[0];
    const { length, width } = rectangleObj[rectangle];
 
    console.log(`The area of ${rectangle} is:
     ${length * width}`);
});


Output

The area of rectangle1 is:
     50
The area of rectangle2 is:
     200
The area of rectangle3 is:
     450


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads