We can loop through an array of objects using JavaScript easily by using the JavaScript Array forEach() method.

arrayOfObjects.forEach(function (eachObject) {
  console.log(eachObject);
});

Let’s take a closer look at this below.


Let’s say we have the following array of objects. Each object will have 3 properties. An id, first name, and last name.

var office_workers = [
  {id: 1, firstName: "John", lastName: "Smith"}, 
  {id: 2, firstName: "Jane", lastName: "Smith"}, 
  {id: 3, firstName: "Nicole", lastName: "Williams"}, 
  {id: 4, firstName: "Bill", lastName: "Brown"}, 
  {id: 5, firstName: "Sam", lastName: "Johnson"}
];

If we wanted to loop through this array and target each object in the array, then we can use our code from above.

Let’s see this in action below.

var office_workers = [
  {id: 1, firstName: "John", lastName: "Smith"}, 
  {id: 2, firstName: "Jane", lastName: "Smith"}, 
  {id: 3, firstName: "Nicole", lastName: "Williams"}, 
  {id: 4, firstName: "Bill", lastName: "Brown"}, 
  {id: 5, firstName: "Sam", lastName: "Johnson"}
];

office_workers.forEach(function (eachObject) {
  console.log(eachObject);
});

#Output
{id: 1, firstName: 'John', lastName: 'Smith'}
{id: 2, firstName: 'Jane', lastName: 'Smith'}
{id: 3, firstName: 'Nicole', lastName: 'Williams'}
{id: 4, firstName: 'Bill', lastName: 'Brown'}
{id: 5, firstName: 'Sam', lastName: 'Johnson'}

Let’s say we wanted to get individual properties for each object. We simply need to target each property in our forEach() method.

Here is how we would get the id, firstName, and lastName properties of each object.

var office_workers = [
  {id: 1, firstName: "John", lastName: "Smith"}, 
  {id: 2, firstName: "Jane", lastName: "Smith"}, 
  {id: 3, firstName: "Nicole", lastName: "Williams"}, 
  {id: 4, firstName: "Bill", lastName: "Brown"}, 
  {id: 5, firstName: "Sam", lastName: "Johnson"}
];

office_workers.forEach(function (eachObject) {
  console.log(eachObject.id);
  console.log(eachObject.firstName);
  console.log(eachObject.lastName);
});

#Output
1
John
Smith
2
Jane
Smith
3
Nicole
Williams
4
Bill
Brown
5
Sam
Johnson

There is another way we can loop through an array of objects.

Using a For Loop to Loop Through an Array of Objects in JavaScript

Another way we can loop through an array of objects using JavaScript is by using a for loop.

for( var i=0; i<arrayOfObjects.length; i++ ){
  console.log(arrayOfObjects[i]);
};

Let’s see the results of this with our same example from above.

var office_workers = [
  {id: 1, firstName: "John", lastName: "Smith"}, 
  {id: 2, firstName: "Jane", lastName: "Smith"}, 
  {id: 3, firstName: "Nicole", lastName: "Williams"}, 
  {id: 4, firstName: "Bill", lastName: "Brown"}, 
  {id: 5, firstName: "Sam", lastName: "Johnson"}
];

for( var i=0; i<office_workers.length; i++ ){
  console.log(office_workers[i]);
};

#Output
{id: 1, firstName: 'John', lastName: 'Smith'}
{id: 2, firstName: 'Jane', lastName: 'Smith'}
{id: 3, firstName: 'Nicole', lastName: 'Williams'}
{id: 4, firstName: 'Bill', lastName: 'Brown'}
{id: 5, firstName: 'Sam', lastName: 'Johnson'}

We can also easily get each property as we did above with the forEach() method.

var office_workers = [
  {id: 1, firstName: "John", lastName: "Smith"}, 
  {id: 2, firstName: "Jane", lastName: "Smith"}, 
  {id: 3, firstName: "Nicole", lastName: "Williams"}, 
  {id: 4, firstName: "Bill", lastName: "Brown"}, 
  {id: 5, firstName: "Sam", lastName: "Johnson"}
];

for( var i=0; i<office_workers.length; i++ ){
  console.log(office_workers[i].id);
  console.log(office_workers[i].firstName);
  console.log(office_workers[i].lastName);
};

#Output
1
John
Smith
2
Jane
Smith
3
Nicole
Williams
4
Bill
Brown
5
Sam
Johnson

We can also loop through these objects starting at the end first, using a reverse for loop.

Hopefully this article has been useful for you to learn how to loop through an array of objects in JavaScript.

Categorized in:

JavaScript,

Last Update: May 3, 2024