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 trialluckius kajoka
1,695 Pointshow can i log out with 4 line contain both proprty and value eg population:value
help me please
var shanghai = {
population: 14.35e6,
longitude: '31.2000 N',
latitude: '121.5000 E',
country: 'CHN'
};
for (var state in shanghai) {
console.log(state,':',shanghai["population"]);
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>JavaScript Objects</title>
</head>
<body>
<script src="script.js"></script>
</body>
</html>
1 Answer
Ross King
20,704 Pointsluckius kajoka You were pretty close!
Problmen
Instead of:
console.log(state,':',shanghai["population"]);
Solution
You need:
console.log(shanghai.state);
Reason
The for in loop iterates through each key in the object. So you have defined the word state as your key and shanghai as the object.
Every iteration of the loop will below will log the state key.
for (var state in shanghai) {
console.log(state);
}
// population
// longitude
// latitude
// country
When combine the key and the object you will get the object properties
for (var state in shanghai) {
console.log(shanghai.state);
}
// 14.35e6
// '31.2000 N'
// '121.5000 E'
// 'CHN'
luckius kajoka
1,695 Pointsluckius kajoka
1,695 Pointsthanks verry much you have opened my mind.