• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / JavaScript / Loop Through an Array of Objects in JavaScript

Loop Through an Array of Objects in JavaScript

August 8, 2022 Leave a Comment

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.

Other Articles You'll Also Like:

  • 1.  Clicking on an Image to Enlarge it in HTML
  • 2.  How to Generate a Random Letter In JavaScript
  • 3.  Using JavaScript to Get URL Hash
  • 4.  Using JavaScript to Run a Function Every 5 seconds
  • 5.  Swapping Images in JavaScript
  • 6.  JavaScript Trunc with Math.trunc() Method
  • 7.  Reverse For Loop JavaScript
  • 8.  Reverse a String in JavaScript
  • 9.  Using JavaScript to Remove Null Values From an Array
  • 10.  Using JavaScript to Get Today’s Date

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy