473,386 Members | 1,668 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Iterating through nested associative arrays

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
3 5129
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
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
Hi All,

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

Your explanations are perfect!

Rob
:)
Jun 27 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

27
by: Abdullah Kauchali | last post by:
Hi folks, Can one rely on the order of keys inserted into an associative Javascript array? For example: var o = new Object(); o = "Adam"; o = "Eve";
6
by: mark4asp | last post by:
Suppose I have the following code. It functions to randomly select a city based upon the probabilities given by the key differences in the associative array. . Eg. because the key difference...
4
by: Robert | last post by:
I am curious why some people feel that Javascript doesn't have associative arrays. I got these definitions of associative arrays via goggle: Arrays in which the indices may be numbers or...
34
by: Christopher Benson-Manica | last post by:
If an array is sparse, say something like var foo=; foo=4; foo='baz'; foo='moo'; is there a way to iterate through the entire array? --
8
by: Derek Basch | last post by:
Is there any way to associate name/value pairs during an array initialization? Like so: sType = "funFilter" filterTypeInfo = ; filterTypeInfo = new Array("type" : sType); I can do it using...
41
by: Rene Nyffenegger | last post by:
Hello everyone. I am not fluent in JavaScript, so I might overlook the obvious. But in all other programming languages that I know and that have associative arrays, or hashes, the elements in...
11
by: Bosconian | last post by:
I'm trying to output the contents of an array of associative arrays in JavaScript. I'm looking for an equivalent of foreach in PHP. Example: var games = new Array(); var teams = new...
2
by: bill | last post by:
The IMAP_headers function returns an associative array. for example: $head = imap_header($in, ($index+1)); echo " <br />header:subject:" . $head->subject; works fine, but $head->from returns...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.