Iterating through nested associative arrays

R

Robert Mark Bram

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
:)
 
E

Erwin Moller

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
 
R

Robin Rattay

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
 
T

Thomas 'PointedEars' Lahn

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
 
R

Robert Mark Bram

Hi All,

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

Your explanations are perfect!

Rob
:)
 

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,744
Messages
2,569,482
Members
44,900
Latest member
Nell636132

Latest Threads

Top