Associative array help needed.

A

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.
 
B

Bert

Ant said:
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
 
A

Ant

Bert said:
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
 
D

Douglas Crockford

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
 
R

RobG

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>
 
R

Randy Webb

RobG wrote:

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.

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

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

RobG

Randy said:
RobG wrote:

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.

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
...
 
D

Douglas Crockford

RobG said:
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;
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
 
R

RobG

Douglas said:
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;
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... :)
 
D

Dr John Stockton

JRS: In article <[email protected]>, dated Sun,
20 Feb 2005 20:53:13, seen in Ant
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.
 

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