Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript JavaScript Loops, Arrays and Objects Tracking Data Using Objects Using `for in` to Loop Through an Object's Properties

Mandy Rintala
Mandy Rintala
9,046 Points

Objective Exercise....

I need help piecing together this objective. It keeps telling me task one is wrong after trying to complete task two. I have tried adding a seperate "for...in" property but that still says I'm messing up task one.

1 Answer

Erik Nuber
Erik Nuber
20,629 Points
for (var prop in shanghai) {
  console.log(prop, ': ', shanghai[prop]);
}

The variable prop can be used to grab each key and value. Alone, prop grabs just the key, which is what part 1 wants. The second question wants you to also log out the value in a specific format key: value.

So this is why we do the prop, ': ', shanghai[prop]. It gives us the format we want.

To log out the property, we need to use the object name, in this case shanghai and then use the property value in square brackets much like we would an index value.

Mandy Rintala
Mandy Rintala
9,046 Points

Ok that makes it a little easier to understand. Thank you for your help!