Loop or Iterate through Array of Objects in JavaScript

📘 Premium Read: Access my best content on Medium member-only articles — deep dives into Java, Spring Boot, Microservices, backend architecture, interview preparation, career advice, and industry-standard best practices.

✅ Some premium posts are free to read — no account needed. Follow me on Medium to stay updated and support my writing.

🎓 Top 10 Udemy Courses (Huge Discount): Explore My Udemy Courses — Learn through real-time, project-based development.

▶️ Subscribe to My YouTube Channel (172K+ subscribers): Java Guides on YouTube

In this tutorial, we are going to learn different ways to loop or iterate through an array of objects in JavaScript.

First way: ForEach method

Let's use es6 provided forEach() method which helps us to iterate over the array of objects:
function demo(){
   let users = [
       {
           firstName: "Ramesh",
           lastName: "Fadatare",
           emailId : "ramesh@gmail.com"
       },
       {
            firstName: "Tony",
            lastName: "Stark",
            emailId : "tony@gmail.com"
        },
        {
            firstName: "Tom",
            lastName: "Cruise",
            emailId : "tom@gmail.com"
        }
   ]

   users.forEach((user)=> {
        console.log(user.firstName)
        console.log(user.lastName)
        console.log(user.emailId)
   });    
}

demo();
Output:
Ramesh
Fadatare
ramesh@gmail.com
Tony
Stark
tony@gmail.com
Tom
Cruise
tom@gmail.com
The forEach method takes the callback function as an argument and runs on each object present in the array.
For the best learning experience, I highly recommended that you open a console (which, in Chrome and Firefox, can be done by pressing Ctrl+Shift+I), navigate to the "console" tab, copy-and-paste each JavaScript code example from this guide, and run it by pressing the Enter/Return key.
Refer below screenshot:

Second way: For of loop

Let's use for of loop to iterate over the array of objects in JavaScript:
function demo1(){
    let users = [
        {
            firstName: "Ramesh",
            lastName: "Fadatare",
            emailId : "ramesh@gmail.com"
        },
        {
             firstName: "Tony",
             lastName: "Stark",
             emailId : "tony@gmail.com"
         },
         {
             firstName: "Tom",
             lastName: "Cruise",
             emailId : "tom@gmail.com"
         }
    ]
 
    for(let user of users){
        console.log(user.firstName)
         console.log(user.lastName)
         console.log(user.emailId)
    }   
 }

demo1();
Output:
Ramesh
Fadatare
ramesh@gmail.com
Tony
Stark
tony@gmail.com
Tom
Cruise
tom@gmail.com

Related JavaScript Examples


Comments

Spring Boot 3 Paid Course Published for Free
on my Java Guides YouTube Channel

Subscribe to my YouTube Channel (165K+ subscribers):
Java Guides Channel

Top 10 My Udemy Courses with Huge Discount:
Udemy Courses - Ramesh Fadatare