problems with json in a for loop

A

Adam

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?
 
V

VK

Adam said:
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); }
 
A

Adam

Thanks! That works great. I totally see what's happening now.
VK said:
Adam said:
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); }
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top