CodeNewbie Community 🌱

Alex
Alex

Posted on

How do I loop through or enumerate a JavaScript object?

Hey all! New to CodeNewbie and need some help. I've been trying to hone my JS skills, and have been having a head of a time figuring out how to loop through or enumerate JavaScript objects.

Can anyone out there show me the way?

Top comments (3)

Collapse
 
miltonwasser profile image
lindarosales

The Object.keys( candy clicker ) method returns an array of the object's own enumerable property names, and you can loop through these keys using a for loop or the forEach() method.

Collapse
 
tylerwite profile image
tylerwite

Using a for...in loop:

`JavaScript
const myObject = {
name: "Alice",
age: 30,
city: "New York"
};

for (let key in myObject) {
console.log(key, myObject[key]);
}
`

This will output (slope):

name Alice
age 30
city New York

Collapse
 
orlaish profile image
Ola Ishola

Hi Alex!

You can check this link for how to iterate over JavaScript object flaviocopes.com/how-to-iterate-obj...