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 Accessing All of the Properties in an Object

nothing is working

i've tried everything

script.js
var shanghai = {
  population: 14.35e6,
  longitude: '31.2000 N',
  latitude: '121.5000 E',
  country: 'CHN'
};for (var something in shanghai){
    console.log(something + ': ' + shanghai[something]);
console.population.shanghai
  console.longitude.shanghai
  console.latitude.shanghai
  console.country.shanghai
} 
index.html
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>JavaScript Objects</title>
</head>
<body>
<script src="script.js"></script>
</body>
</html>

3 Answers

The second part you need to get the key, this is done by the object name with square brackets for each property like so

var shanghai = {
  population: 14.35e6,
  longitude: '31.2000 N',
  latitude: '121.5000 E',
  country: 'CHN'
};

for (var property in shanghai ) {
  console.log(property + ": " + shanghai[property]);
}

further reading

Good Luck

Hi Kim

You got the right idea but with the for loop but the variable you use is the looping through the properties.

So,

var shanghai = {
  population: 14.35e6,
  longitude: '31.2000 N',
  latitude: '121.5000 E',
  country: 'CHN'
};

for (var property in shanghai ) {     // for the properties in shanghai object
  console.log(property);              // log each property
}

Good Luck

Hi Liam. The first part works fine. but the second part, nothing works unless i've got it out of line. i've tried doing all 4 lines of each property but it still isn't working. I just have this one last one left and nothing is working. I've tried everyone's suggestions.

thank you Liam. I need an actual teacher. I'm having a difficult time with retention of this.