HighDots Forums  

Iterating through nested associative arrays

Javascript JavaScript language (comp.lang.javascript)


Discuss Iterating through nested associative arrays in the Javascript forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Robert Mark Bram
 
Posts: n/a

Default Iterating through nested associative arrays - 05-23-2008 , 03:48 AM






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


Reply With Quote
  #2  
Old   
Erwin Moller
 
Posts: n/a

Default Re: Iterating through nested associative arrays - 05-23-2008 , 04:43 AM






Robert Mark Bram schreef:
Quote:
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


Quote:
Rob


Reply With Quote
  #3  
Old   
Robin Rattay
 
Posts: n/a

Default Re: Iterating through nested associative arrays - 05-23-2008 , 05:07 AM



On 23 Mai, 10:48, Robert Mark Bram <robertmarkb... (AT) gmail (DOT) com> wrote:
Quote:
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.

Quote:
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 = {};

Quote:
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"
}
};

Quote:
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


Reply With Quote
  #4  
Old   
Thomas 'PointedEars' Lahn
 
Posts: n/a

Default Re: Iterating through nested associative arrays - 05-23-2008 , 05:32 AM



Robert Mark Bram wrote:
Quote:
I have some code to iterate through nested associative arrays.
No, you don't. There is no such thing in ECMAScript implementations.

Quote:
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.

Quote:
var dealer1 = new Array();
var dealer1 = new Object();
or
var dealer1 = Object();

Quote:
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"
};

Quote:
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[i].label + " - " + dealers[i].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
--
realism: HTML 4.01 Strict
evangelism: XHTML 1.0 Strict
madness: XHTML 1.1 as application/xhtml+xml
-- Bjoern Hoehrmann


Reply With Quote
  #5  
Old   
Robert Mark Bram
 
Posts: n/a

Default Re: Iterating through nested associative arrays - 05-23-2008 , 08:20 PM



Hi All,

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

Your explanations are perfect!

Rob


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.