473,404 Members | 2,195 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,404 software developers and data experts.

Associative array help needed.

Ant
Hi,

I'm using an associative array to simulate a multidimensional array like
this:

myArray['00'] = "John";
myArray['01'] = "35";
myArray['02'] = "12345";
myArray['03'] = "01/01/1975";

myArray['10'] = "Sarah";
myArray['11'] = "29";
myArray['12'] = "56789";
myArray['13'] = "12/12/19";

I'm trying to work out how to loop through the entire array and I'm having
some problems.

for (var i = 0; i < arrayCount; i++)
{
for (var j = 0; j < attribCount; j++)
{
alert(myArray[i + j]);
}
}

Now this doesn't work as it's adding the i and j together rather than
joining them. And it doesn't work when I do 'i' and 'j' so I'm wondering, is
it actually possible to loop through the array?

Thanks for any help.
Jul 23 '05 #1
10 1409

"Ant" <no*****@nospam.com> schreef in bericht
news:cv**********@wisteria.csv.warwick.ac.uk...
Hi,

I'm using an associative array to simulate a multidimensional array like
this:

myArray['00'] = "John";
myArray['01'] = "35";
myArray['02'] = "12345";
myArray['03'] = "01/01/1975";

myArray['10'] = "Sarah";
myArray['11'] = "29";
myArray['12'] = "56789";
myArray['13'] = "12/12/19";

I'm trying to work out how to loop through the entire array and I'm having
some problems.

for (var i = 0; i < arrayCount; i++)
{
for (var j = 0; j < attribCount; j++)
{
alert(myArray[i + j]);
}
}

Now this doesn't work as it's adding the i and j together rather than
joining them. And it doesn't work when I do 'i' and 'j' so I'm wondering,
is it actually possible to loop through the array?

Thanks for any help.


try:

for (var i = 0; i < arrayCount; i++)
{
for (var j = 0; j < attribCount; j++)
{
alert(myArray[i*10 + j]);
}
}

maybe for (var i = 0; i < arrayCount/10; i++)

but I don't know what arraycount is....

grt Bert
Jul 23 '05 #2
Ant
"Bert" <b.******@chello.nl> wrote in message >
try:

for (var i = 0; i < arrayCount; i++)
{
for (var j = 0; j < attribCount; j++)
{
alert(myArray[i*10 + j]);
}
}

maybe for (var i = 0; i < arrayCount/10; i++)

but I don't know what arraycount is....

grt Bert


That's really clever, I wish I'd thought of that.

Ta
Jul 23 '05 #3
> I'm using an associative array to simulate a multidimensional array like
this:

myArray['00'] = "John";
myArray['01'] = "35";
myArray['02'] = "12345";
myArray['03'] = "01/01/1975";

myArray['10'] = "Sarah";
myArray['11'] = "29";
myArray['12'] = "56789";
myArray['13'] = "12/12/19";

I'm trying to work out how to loop through the entire array and I'm having
some problems.


There is a better representaion for this kind of data: an array of objects:

myArray = [{
name: "John",
age: "35",
number: "12345",
date: "01/01/1975"},
{
name: "Sarah",
age: "29",
number: "56789",
date: "12/12/19"}];

http://www.JSON.org
Jul 23 '05 #4
Douglas Crockford wrote:
[...]
I'm trying to work out how to loop through the entire array and I'm
having some problems.

There is a better representaion for this kind of data: an array of objects:

myArray = [{
name: "John",
age: "35",
number: "12345",
date: "01/01/1975"},
{
name: "Sarah",
age: "29",
number: "56789",
date: "12/12/19"}];

http://www.JSON.org


Yes Douglas - but the rest of the question was how to loop
through the array. My guess is that the OP will attempt to call
each element explicitly, but what about getting all of them
without needing to know what they are called?

So I had a go at looping through all the elements of all the
objects, but I'm left with two questions (hopefully I've used
do/while correctly!):

1. Is there a way to avoid using eval?

2. Is there an efficient way of putting the j decrement inside
the while, but still get the zero-th element? I could have
used:

j = myArray.length;
...
eval('myArray[j-1].' + anAttribute)
...
while(--j)

but (without testing) all those [j-1]'s seem inefficient.
<script type="text/javascript">
myArray = [{
name: "John",
age: "35",
number: "12345",
date: "01/01/1975"},
{
name: "Sarah",
age: "29",
number: "56789",
date: "12/12/19"}];

function sayArray() {
var msg='',
anAttribute,
j;
if (myArray) {
j = myArray.length - 1;

do {
msg += '\nEntry: ' + j;
for ( anAttribute in myArray[j]){
msg += '\n ' + anAttribute + ': '
+ eval('myArray[j].' + anAttribute);
}
--j;
} while(j >= 0);

}
alert(msg);
}
<button onclick="sayArray();">Say myArray</button>

--
Rob
Jul 23 '05 #5
RobG wrote:

<--snip-->
1. Is there a way to avoid using eval?
Yes. Arrays (and Objects) are properties of the window object and
accessible via the Array Notation.

<--snip-->
msg += '\n ' + anAttribute + ': '
+ eval('myArray[j].' + anAttribute);


msg += '\n + anAttribute + ': '
+ window['myArray[j].' + anAttribute];

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Jul 23 '05 #6
Randy Webb wrote:
RobG wrote:

<--snip-->
1. Is there a way to avoid using eval?

Yes. Arrays (and Objects) are properties of the window object and
accessible via the Array Notation.

<--snip-->
msg += '\n ' + anAttribute + ': '
+ eval('myArray[j].' + anAttribute);

msg += '\n + anAttribute + ': '
+ window['myArray[j].' + anAttribute];


Thanks Randy, I saw your answer to the cycling through functions
post so I tried using the window object. I used every
combination of square brackets & quotes I could think of
(including your suggestion) but in both IE and Firefox it comes
out as:

Entry: 1
name: undefined
age: undefined
...
--
Rob
Jul 23 '05 #7
RobG wrote:
Douglas Crockford wrote:
[...]
I'm trying to work out how to loop through the entire array and I'm
having some problems.


There is a better representaion for this kind of data: an array of
objects:

myArray = [{
name: "John",
age: "35",
number: "12345",
date: "01/01/1975"},
{
name: "Sarah",
age: "29",
number: "56789",
date: "12/12/19"}];

http://www.JSON.org

Yes Douglas - but the rest of the question was how to loop
through the array. My guess is that the OP will attempt to call
each element explicitly, but what about getting all of them
without needing to know what they are called?

So I had a go at looping through all the elements of all the
objects, but I'm left with two questions (hopefully I've used
do/while correctly!):

1. Is there a way to avoid using eval?

2. Is there an efficient way of putting the j decrement inside
the while, but still get the zero-th element? I could have
used:

j = myArray.length;
...
eval('myArray[j-1].' + anAttribute)
...
while(--j)

but (without testing) all those [j-1]'s seem inefficient.
<script type="text/javascript">
myArray = [{
name: "John",
age: "35",
number: "12345",
date: "01/01/1975"},
{
name: "Sarah",
age: "29",
number: "56789",
date: "12/12/19"}];

function sayArray() {
var msg='',
anAttribute,
j;
if (myArray) {
j = myArray.length - 1;

do {
msg += '\nEntry: ' + j;
for ( anAttribute in myArray[j]){
msg += '\n ' + anAttribute + ': '
+ eval('myArray[j].' + anAttribute);
}
--j;
} while(j >= 0);

}
alert(msg);
}
<button onclick="sayArray();">Say myArray</button>


That use of eval is totally and completely wrongheaded. Do it right:

var i, k, o;
var msg = '';
for (i = 0; i < myArray.length; i += 1) {
msg += 'Record: ' + i + '\n';
o = myArray[i];
for (k in o) {
msg += ' ' + k + ': ' + o[k] '\n';
}
}

If you ever find yourself using eval, you can be certain that you are
doing it wrong.

Who taught you to program like that? Where did you learn to use eval so
badly? The guilty must be punished.

http://www.crockford.com/javascript/survey.html
Jul 23 '05 #8
Douglas Crockford wrote:
RobG wrote:
[...]
So I had a go at looping through all the elements of all the
objects, but I'm left with two questions (hopefully I've used
do/while correctly!):

1. Is there a way to avoid using eval?

2. Is there an efficient way of putting the j decrement inside
the while, but still get the zero-th element? I could have
used:

[...]
That use of eval is totally and completely wrongheaded. Do it right:

var i, k, o;
var msg = '';
for (i = 0; i < myArray.length; i += 1) {
msg += 'Record: ' + i + '\n';
o = myArray[i];
for (k in o) {
msg += ' ' + k + ': ' + o[k] '\n';
}
}

If you ever find yourself using eval, you can be certain that you are
doing it wrong.

Who taught you to program like that? Where did you learn to use eval so
badly? The guilty must be punished.

http://www.crockford.com/javascript/survey.html


I apologise most sincerely - the fervent dislike of eval by the
regulars of this new group is, well, bleedin' obvious, hence my
question.

Fortunately, there seems to be more tolerance of:

... i < myArray.length; ... // slow when i is large?
... i += 1) { // is i++ out of favour too?
... + o[k] '\n'; // Ooops... + o[k] + '\n' works.
To err is human... :-)

--
Rob
Jul 23 '05 #9
JRS: In article <cv**********@wisteria.csv.warwick.ac.uk>, dated Sun,
20 Feb 2005 20:53:13, seen in news:comp.lang.javascript, Ant
<no*****@nospam.com> posted :
myArray['00'] = "John";
myArray['01'] = "35";
myArray['02'] = "12345";
myArray['03'] = "01/01/1975";

myArray['10'] = "Sarah";
myArray['11'] = "29";
myArray['12'] = "56789";
myArray['13'] = "12/12/19";
That's likely to be 1912/2012 December 19. Format YYYY/MM/DD should be
safe. Never use a date format that can be taken or mistaken for
MM/DD/YY[YY].

I'm trying to work out how to loop through the entire array and I'm having
some problems.

for (var i = 0; i < arrayCount; i++)
{
for (var j = 0; j < attribCount; j++)
{
alert(myArray[i + j]);
}
}

Now this doesn't work as it's adding the i and j together rather than
joining them.


i + '' + j // should join them

Operator + will concatenate if both arguments are strings, or if one is
string and the other is number; if both are number it will add. Other
types will be preconverted to number or to string.

Note too the other comments. See below.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Jul 23 '05 #10

Randy Webb wrote:
RobG wrote:

<--snip-->
1. Is there a way to avoid using eval?


Yes. Arrays (and Objects) are properties of the window object and
accessible via the Array Notation.


(snip)

Only if they're stored as 'global' variables. Nothing native about
this.

http://jibbering.com/faq/faq_notes/square_brackets.html

Jul 23 '05 #11

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...
2
by: Remi Bastide | last post by:
Is is possible to initialize a javascript associative array inline, as you would do in PHP, e.g. : <?php $a = array("abc" => "def", "ghi" => "jkl"); ?>
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...
47
by: VK | last post by:
Or why I just did myArray = "Computers" but myArray.length is showing 0. What a hey? There is a new trend to treat arrays and hashes as they were some variations of the same thing. But they...
7
by: Robert Mark Bram | last post by:
Hi All! How do you get the length of an associative array? var my_cars= new Array() my_cars="Mustang"; my_cars="Station Wagon"; my_cars="SUV"; alert(my_cars.length);
1
by: swayze | last post by:
Hi there, We're using php 4.1 and it doesn't seem to have built in support for this. Coming from a dotnet background this surprised me...Anyways, thats a different topic altogether... I'm...
13
by: Felix H. Dahlke | last post by:
Have been hacking on this for about an hour, so I thought that this effort was useless if noone would use it. The following code generally is a hack-example of the implementation of an associative...
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...
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?
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,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.