Iteration through a json object

G

Gregor Kofler

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
 
N

Nick S

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
 
N

Nick S

Nick S meinte:



What do you mean by "json object"?







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
 
G

Gregor Kofler

Nick S meinte:


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?

Gregor
 
N

Nick S

Nick S meinte:

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.
 
G

Gregor Kofler

Nick S meinte:
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
 

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

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top