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

Iterating through nested associative arrays

P: n/a
Hi all,

I have some code to iterate through nested associative arrays. Can
anyone please tell me what I am doing wrong? :)

var dealers = new Array();

var dealer1 = new Array();
dealer1["label"] = "Jefferson Ford Pty Ltd";
dealer1["address"] = "215-217 Normanby Rd South Melbourne VIC 3205";
dealers["dealer1"] = dealer1;

var dealer2 = new Array();
dealer2["label"] = "Freeway Ford";
dealer2["address"] = "290 South Gippsland Hwy Cranbourne VIC 3977";
dealers["dealer2"] = dealer2;

for (dealer in dealers) {
alert(dealer["label"] + " - " + dealer["address"]);
}

The alerts are all showing "undefined - undefined".

Thank you for any assistance!

Rob
:)
Jun 27 '08 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Robert Mark Bram schreef:
Hi all,

I have some code to iterate through nested associative arrays. Can
anyone please tell me what I am doing wrong? :)

var dealers = new Array();

var dealer1 = new Array();
dealer1["label"] = "Jefferson Ford Pty Ltd";
dealer1["address"] = "215-217 Normanby Rd South Melbourne VIC 3205";
dealers["dealer1"] = dealer1;

var dealer2 = new Array();
dealer2["label"] = "Freeway Ford";
dealer2["address"] = "290 South Gippsland Hwy Cranbourne VIC 3977";
dealers["dealer2"] = dealer2;

for (dealer in dealers) {
alert(dealer["label"] + " - " + dealer["address"]);
}

The alerts are all showing "undefined - undefined".

Thank you for any assistance!
Hi,

JavaScript Arrays do not use hashed keys (strings).
Your Javascript looks more like PHP to me then JavaScript. ;-)

Didn't it give you errors in your errorconsole?

You need an Object to mimic that behaviour.

Try using new Object() instead of new Array(), and you can use strings
as keys for your array.
The 'keys' are named 'properties' of the object in JavaScript (I think).

Regards,
Erwin Moller

>
Rob
:)
Jun 27 '08 #2

P: n/a
On 23 Mai, 10:48, Robert Mark Bram <robertmarkb...@gmail.comwrote:
I have some code to iterate through nested associative arrays.
For the first thing: There are no such things as "associative arrays"
in JavaScript. However user-defined object properties can be used as
such, just as you are doing.
var dealers = new Array();
Since object properties have nothing to do with JavaScript arrays, it
is better to create a new basic Object instead of an Array:

var dealers = new Object();

or if using an object literal

var dealers = {};
var dealer1 = new Array();
dealer1["label"] = "Jefferson Ford Pty Ltd";
dealer1["address"] = "215-217 Normanby Rd South Melbourne VIC 3205";
dealers["dealer1"] = dealer1;

var dealer2 = new Array();
dealer2["label"] = "Freeway Ford";
dealer2["address"] = "290 South Gippsland Hwy Cranbourne VIC 3977";
dealers["dealer2"] = dealer2;
If you actually hardcoding this information it is probably simpler to
use full object literals:

var dealers = {
dealer1: {
"label": "Jefferson Ford Pty Ltd",
"adress": "215-217 Normanby Rd South Melbourne VIC 3205"
},
dealer2: {
"label": "Freeway Ford";
"address": "290 South Gippsland Hwy Cranbourne VIC 3977"
}
};
for (dealer in dealers) {
alert(dealer["label"] + " - " + dealer["address"]);
}
for...in returns the property name, not the actual object in the
variable:

for (dealer in dealers) {
alert(dealer);
alert(dealers[dealer]["label"] + " - " + dealers[dealer]
["address"]);
}

Robin
Jun 27 '08 #3

P: n/a
Hi All,

Thank you very much Erwin, Robin and Thomas, err, PointedEars :)

Your explanations are perfect!

Rob
:)
Jun 27 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.