Robert said:
I have some code to iterate through nested associative arrays.
No, you don't. There is no such thing in ECMAScript implementations.
Can anyone please tell me what I am doing wrong?
var dealers = new Array();
Apparently you don't need array properties here, so you should get rid of
the Array object overhead and use
var dealers = new Object();
or
var dealers = Object();
instead.
var dealer1 = new Array();
var dealer1 = new Object();
or
var dealer1 = Object();
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;
That is much too complicated with no obvious advantage.
Variant A (Array of Object objects):
var dealers = new Array(
{
label: "Jefferson Ford Pty Ltd",
address: "215-217 Normanby Rd South Melbourne VIC 3205"
},
{
label: "Freeway Ford",
address: "290 South Gippsland Hwy Cranbourne VIC 3977"
}
);
(Instead of `new Array(...)' you may also use the Array initializer
`[...]' which is available in newer ECMAScript implementations. See
http://PointedEars.de/scripts/es-matrix.)
However, lookup of the address is easier if you use the label as property
name, provided it is unique in the recordset. Which leads to
Variant B (Object object with references to Object objects as property values):
var dealers = {
"Jefferson Ford Pty Ltd": {
address: "215-217 Normanby Rd South Melbourne VIC 3205"
},
"Freeway Ford": {
address: "290 South Gippsland Hwy Cranbourne VIC 3977"
}
};
The wrapping inner object is only for convenience, should you want to store
other values related to the label in the foreseeable future. Otherwise you
can use the address as the property value itself. Therefore,
Variant C (Object object with strings as property values):
var dealers = {
"Jefferson Ford Pty Ltd":
"215-217 Normanby Rd South Melbourne VIC 3205",
"Freeway Ford":
"290 South Gippsland Hwy Cranbourne VIC 3977"
};
for (dealer in dealers) {
alert(dealer["label"] + " - " + dealer["address"]);
}
The alerts are all showing "undefined - undefined".
Because (unlike `foreach' in PHP where you may be coming from) for-in loops
always iterate over the object's properties; `dealer' holds the property
name, a string value, and string objects usually don't have a `label' or
`address' property.
Probably you were looking for
for (dealer in dealers)
{
window.alert(dealers[dealer].label + " - " + dealers[dealer].address);
}
(Note that although I could, I am not using the bracket property accessor
here always which should indicate to you that this has nothing to do with
arrays even if you use Array objects.)
However, with the suggestions above you should use (Variant A)
for (var i = 0, len = dealers.length; i < len; i++)
{
window.alert(dealers
.label + " - " + dealers.address);
}
or (Variant B)
for (var dealer in dealers)
{
window.alert(dealer + " - " + dealers[dealer].address);
}
or (Variant C)
for (var dealer in dealers)
{
window.alert(dealer + " - " + dealers[dealer]);
}
instead. Note that with for-in iteration order is not guaranteed.
HTH
PointedEars