By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,166 Members | 1,130 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,166 IT Pros & Developers. It's quick & easy.

problems with json in a for loop

P: n/a
I'm trying to retrieve some values from a json object, but instead it's
giving me the property name. For example:

var json = { "glossary": { "title": "example glossary" } };
console.log(json);
alert(json.glossary.title);
for (var x in json) { console.log(x); alert(x.title); }

This will show me the json object in the console with glossary and
title underneath it. When the alert for json.glossary.title fires, it
will show me "example glossary". however, inside of the for loop, I
see "glossary" in the console log (instead of the glossary object) and
undefined for the x.title alert.

What's going on? Could someone please explain why it's behaving this
way?

Dec 7 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
VK

Adam wrote:
I'm trying to retrieve some values from a json object, but instead it's
giving me the property name. For example:

var json = { "glossary": { "title": "example glossary" } };
console.log(json);
alert(json.glossary.title);
for (var x in json) { console.log(x); alert(x.title); }

This will show me the json object in the console with glossary and
title underneath it. When the alert for json.glossary.title fires, it
will show me "example glossary". however, inside of the for loop, I
see "glossary" in the console log (instead of the glossary object) and
undefined for the x.title alert.

What's going on? Could someone please explain why it's behaving this
way?
That will become much clearer if you try alert(typeof x); which gives
you "string".

That is a common misconception about for-in loop that it returns
enumerable *properties* of the object. I saw it written even in some
overall good manuals.

for-in loops returns string *names* of enumerable properties in the
given object.

var json = { "glossary": { "title": "example glossary" } };
alert(json);
alert(json.glossary.title);
for (var x in json) { alert(x); alert(json[x].title); }

Dec 7 '06 #2

P: n/a
Thanks! That works great. I totally see what's happening now.
VK wrote:
Adam wrote:
I'm trying to retrieve some values from a json object, but instead it's
giving me the property name. For example:

var json = { "glossary": { "title": "example glossary" } };
console.log(json);
alert(json.glossary.title);
for (var x in json) { console.log(x); alert(x.title); }

This will show me the json object in the console with glossary and
title underneath it. When the alert for json.glossary.title fires, it
will show me "example glossary". however, inside of the for loop, I
see "glossary" in the console log (instead of the glossary object) and
undefined for the x.title alert.

What's going on? Could someone please explain why it's behaving this
way?

That will become much clearer if you try alert(typeof x); which gives
you "string".

That is a common misconception about for-in loop that it returns
enumerable *properties* of the object. I saw it written even in some
overall good manuals.

for-in loops returns string *names* of enumerable properties in the
given object.

var json = { "glossary": { "title": "example glossary" } };
alert(json);
alert(json.glossary.title);
for (var x in json) { alert(x); alert(json[x].title); }
Dec 7 '06 #3

P: n/a
I'm trying to retrieve some values from a json object, but instead it's
giving me the property name. For example:

var json = { "glossary": { "title": "example glossary" } };
console.log(json);
alert(json.glossary.title);
for (var x in json) { console.log(x); alert(x.title); }

This will show me the json object in the console with glossary and
title underneath it. When the alert for json.glossary.title fires, it
will show me "example glossary". however, inside of the for loop, I
see "glossary" in the console log (instead of the glossary object) and
undefined for the x.title alert.

What's going on? Could someone please explain why it's behaving this
way?
This might be helpful: http://yuiblog.com/blog/2006/09/26/for-in-intrigue/
Dec 7 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.