|  | Iteration through a json object |  | |
| | | Nick S |  |
| Posted: Fri Sep 05, 2008 12:20 pm Post subject: Iteration through a json object |  |
Hi
Is there anyway to iterate through a json object and retrieve the name of the property as well as the value.
For exampe. Imagine this object
{ title: '1', bedrooms: '2', bathrooms: '3', description: '4', country: '5', location: '6', price: '7', }
I want to loop through it and have a variable that holds title, then bedrooms then bathrooms.... as well as one that holds 1 then 2 then 3... respectively.
Is that possible?
Thanks |
| |
| | | Gregor Kofler |  |
| Posted: Fri Sep 05, 2008 12:20 pm Post subject: Re: Iteration through a json object |  |
Nick S meinte:
| Quote: | Hi
Is there anyway to iterate through a json object and retrieve the name of the property as well as the value.
|
What do you mean by "json object"?
| Quote: | For exampe. Imagine this object
{ title: '1', bedrooms: '2', bathrooms: '3', description: '4', country: '5', location: '6', price: '7', }
I want to loop through it and have a variable that holds title, then bedrooms then bathrooms.... as well as one that holds 1 then 2 then 3... respectively.
Is that possible?
|
You are looking for "for ... in". Mind the possible problems with (augmented) prototypes, though.
Gregor
-- LINK ::: Landschafts- und Reisefotografie LINK ::: meine JS-Spielwiese LINK ::: Bildagentur für den alpinen Raum |
| |
| | | Gregor Kofler |  |
| Posted: Fri Sep 05, 2008 12:20 pm Post subject: Re: Iteration through a json object |  |
Nick S meinte:
| Quote: | --http://photo.gregorkofler.at::: Landschafts- und LINK ::: meine JS-Spielwiesehttp://www.image2d.com ::: Bildagentur für den alpinen Raum
|
Sigh. Please don't quote sigs...
| Quote: | err, ok, just forget my crappy grasp of the terminology for a second. I've outlined the problem pretty clearly. I do use "for...in" but I can't get to the name of each item. (And if that's not clear I mean the thing on the left of the colon) 
|
Lets see:
var obj = { ... }, p; for(p in obj) { window.alert(obj[p]); };
Please answer: What is the value of p?
Gregor
-- LINK ::: Landschafts- und Reisefotografie LINK ::: meine JS-Spielwiese LINK ::: Bildagentur für den alpinen Raum |
| |
| | | Gregor Kofler |  |
| Posted: Fri Sep 05, 2008 12:20 pm Post subject: Re: Iteration through a json object |  |
Nick S meinte:
| Quote: | On Sep 5, 3:10 pm, Gregor Kofler <use...@gregorkofler.at> wrote:
var obj = { ... }, p; for(p in obj) { window.alert(obj[p]);
};
Please answer: What is the value of p?
Thanks. yes, this is what I do, however, I don't just want the value, I also want the name of the property.
|
Jeez! The value of p *is* the name of the property. obj[p] gives you the value of the property p.
Gregor
-- LINK ::: Landschafts- und Reisefotografie LINK ::: meine JS-Spielwiese LINK ::: Bildagentur für den alpinen Raum |
| |
| | | Nick S |  |
| Posted: Fri Sep 05, 2008 12:43 pm Post subject: Re: Iteration through a json object |  |
| |  | |
On Sep 5, 2:18 pm, Gregor Kofler <use...@gregorkofler.at> wrote:
| Quote: | Nick S meinte:
Hi
Is there anyway to iterate through a json object and retrieve the name of the property as well as the value.
What do you mean by "json object"?
For exampe. Imagine this object
{ title: '1', bedrooms: '2', bathrooms: '3', description: '4', country: '5', location: '6', price: '7', }
I want to loop through it and have a variable that holds title, then bedrooms then bathrooms.... as well as one that holds 1 then 2 then 3... respectively.
Is that possible?
You are looking for "for ... in". Mind the possible problems with (augmented) prototypes, though.
Gregor
--http://photo.gregorkofler.at::: Landschafts- und LINK ::: meine JS-Spielwiesehttp://www.image2d.com ::: Bildagentur für den alpinen Raum
|
err, ok, just forget my crappy grasp of the terminology for a second. I've outlined the problem pretty clearly. I do use "for...in" but I can't get to the name of each item. (And if that's not clear I mean the thing on the left of the colon) :)
Thanks |
| |
| | | Nick S |  |
| Posted: Fri Sep 05, 2008 1:48 pm Post subject: Re: Iteration through a json object |  |
On Sep 5, 3:10 pm, Gregor Kofler <use...@gregorkofler.at> wrote:
| Quote: | Nick S meinte:
--http://photo.gregorkofler.at:::Landschafts- und LINK meine JS-Spielwiesehttp://www.image2d.com ::: Bildagentur für den alpinen Raum
Sigh. Please don't quote sigs...
err, ok, just forget my crappy grasp of the terminology for a second. I've outlined the problem pretty clearly. I do use "for...in" but I can't get to the name of each item. (And if that's not clear I mean the thing on the left of the colon) :)
Lets see:
var obj = { ... }, p; for(p in obj) { window.alert(obj[p]);
};
Please answer: What is the value of p?
|
Thanks. yes, this is what I do, however, I don't just want the value, I also want the name of the property.
| Quote: | Gregor
--http://photo.gregorkofler.at::: Landschafts- und LINK ::: meine JS-Spielwiesehttp://www.image2d.com ::: Bildagentur für den alpinen Raum |
|
| |
| | | Nick S |  |
| Posted: Fri Sep 05, 2008 2:18 pm Post subject: Re: Iteration through a json object |  |
On Sep 5, 4:02 pm, Gregor Kofler <use...@gregorkofler.at> wrote:
| Quote: | Nick S meinte:
On Sep 5, 3:10 pm, Gregor Kofler <use...@gregorkofler.at> wrote: var obj = { ... }, p; for(p in obj) { window.alert(obj[p]);
};
Please answer: What is the value of p?
Thanks. yes, this is what I do, however, I don't just want the value, I also want the name of the property.
Jeez! The value of p *is* the name of the property. obj[p] gives you the value of the property p.
Gregor
--http://photo.gregorkofler.at::: Landschafts- und LINK ::: meine JS-Spielwiesehttp://www.image2d.com ::: Bildagentur für den alpinen Raum
|
he he ok ok, I've been at this for hours (not the problem, the app) and I'm a little bog eyed Thanks for the help. Even if it had a dose of sarcasm in it  |
| |
|
|