Languages
[Edit]
EN

JavaScript - loop through array of objects

0 points
Created by:
Cassia-Mendoza
415

In this article, we would like to show you how to loop through array of objects in JavaScript.

Quick solution:

// ONLINE-RUNNER:browser;

const users = [
	{ id: 1, name: 'Kate', age: 25 },
	{ id: 2, name: 'Tom',  age: 23 }
];

// display each user name
users.map(user => console.log(user.name));

Note:

In this solution we used map() to iterate through the users array, but you can use any other method from the following article:

 

1. Alternative solution using forEach()

In this example, we pass an arrow function to the forEach() method to display name property value of each user in the users array.

// ONLINE-RUNNER:browser;

const users = [
	{ id: 1, name: 'Kate', age: 25 },
	{ id: 2, name: 'Tom',  age: 23 },
	{ id: 3, name: 'Ann',  age: 26 },
	{ id: 4, name: 'Jack', age: 21 }
];

// loop through array of objects
users.forEach((user) => {
    console.log(user.name);
})

Output:

Kate
Tom
Ann
Jack
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join