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 | | | | 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
-- http://photo.gregorkofler.at ::: Landschafts- und Reisefotografie http://web.gregorkofler.com ::: meine JS-Spielwiese http://www.image2d.com ::: Bildagentur für den alpinen Raum | | | | re: Iteration through a json object
On Sep 5, 2:18 pm, Gregor Kofler <use...@gregorkofler.atwrote: Quote:
Nick S meinte:
> > Quote:
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
> Quote:
{
title: '1',
bedrooms: '2',
bathrooms: '3',
description: '4',
country: '5',
location: '6',
price: '7',
}
> Quote:
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.
> >
You are looking for "for ... in". Mind the possible problems with
(augmented) prototypes, though.
>
Gregor
>
--http://photo.gregorkofler.at::: Landschafts- und Reisefotografiehttp://web.gregorkofler.com ::: 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 | | | | re: Iteration through a json object
Nick S meinte: Quote: Quote:
>--http://photo.gregorkofler.at::: Landschafts- und Reisefotografiehttp://web.gregorkofler.com ::: 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
-- http://photo.gregorkofler.at ::: Landschafts- und Reisefotografie http://web.gregorkofler.com ::: meine JS-Spielwiese http://www.image2d.com ::: Bildagentur für den alpinen Raum | | | | re: Iteration through a json object
On Sep 5, 3:10 pm, Gregor Kofler <use...@gregorkofler.atwrote: Quote:
Nick S meinte:
> Quote: Quote:
--http://photo.gregorkofler.at:::Landschafts- und Reisefotografiehttp://web.gregorkofler.com::: 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?
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 Reisefotografiehttp://web.gregorkofler.com ::: meine JS-Spielwiesehttp://www.image2d.com :::Bildagentur für den alpinen Raum
| | | | re: Iteration through a json object
Nick S meinte: Quote:
On Sep 5, 3:10 pm, Gregor Kofler <use...@gregorkofler.atwrote:
Quote: Quote:
>var obj = { ... }, p;
>for(p in obj) {
> window.alert(obj[p]);
>>
>};
>>
>Please answer: What is the value of p?
Quote:
>
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 Reisefotografie http://web.gregorkofler.com ::: meine JS-Spielwiese http://www.image2d.com ::: Bildagentur für den alpinen Raum |  | Similar JavaScript / Ajax / DHTML bytes | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,531 network members.
|