Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

# Thread: Multidimensional / Associative arrays

1. ## Multidimensional / Associative arrays

hi,

I will try to explain what I am trying to do as best as I can, however I think it will be much easier to follow along if you can see a demo.

Okay, so I am trying to work with arrays.

Code:
```
var a = new Array('a1', 'a2');
a['a1'] = 'a1value';
a['a2'] = 'a2value';
var b = new Array('b1', 'b2');
b['b1'] = 'b1value';
b['b2'] = 'b2value';
var ab = new Array(a, b);```
When I try to access the array directly, I get the associative name ('a1', 'a2', 'b1', or 'b2') not the value ('a1value', 'a2value', 'b1value', 'b2value').

Code:
```
alert('Value of ab[0][1]:\n\n' + ab[0][1]);
//should return 'a2value', but instead it returns 'a2'.```
At first I thought I was trying to store the value into the variable incorrectly.
However, when I use for(value in array) loops, I get the associative name half the time, but the other half the time I can actually get the value. This means that the value is in the variable, but I don't know how to properly retrieve it.

Here are my for loops:
Code:
```
for(value1 in ab){
for(value2 in ab[value1]){
}
}```
Another question, why is my second for loop looping 4 times through an array that only has a length of 2?

This is very confusing, and I don't understand what is happening...
Again, here is the demo to show you what I am trying to do.

Any help is greatly appreciated!!!

~Brandon

• Originally Posted by 333qaz333
Code:
```
var a = new Array('a1', 'a2');
a['a1'] = 'a1value';
a['a2'] = 'a2value';
var b = new Array('b1', 'b2');
b['b1'] = 'b1value';
b['b2'] = 'b2value';
var ab = new Array(a, b);```
When I try to access the array directly, I get the associative name ('a1', 'a2', 'b1', or 'b2') not the value ('a1value', 'a2value', 'b1value', 'b2value').
Elements created using associative syntax are stored entirely separately, they cannot be indexed numerically and their existence is not reflected in the length property.

• I'll use array shorthand. Aka:
Code:
```//longhand
var x = new Array(1, 2, 3, . . . );
//shorthand
var x = [1, 2, 3, . . . ];```
*****************

Okay, so
Code:
```var a = ["a1", "a2"];

//Same as
var a = [];
a[0] = "a1";
a[1] = "a2";```

Given
Code:
```var a = ["a1", "a2"];
a["a1"] = "a1value";
a["a2"] = "a2value";
var b = ["b1", "b2"];
b["b1"] = "b1value";
b["b2"] = "b2value";
var ab = [a, b];```
a looks like:

Code:
```               a
|
-----------------------------
|      |        |           |
|      |        |           |
[0]    [1]      [a1]        [a2]
("a1") ("a2") ("a1value") ("a2value")```
b looks similar.

ab looks like:
Code:
```    ab
|
--------
|      |
[0]    [1]
(a)    (b)
|      |
...    ...```

• ## The Following 2 Users Say Thank You to Trinithis For This Useful Post:

333qaz333 (12-30-2007), sybil6 (01-01-2008)

• THANK YOU TRINITHIS!!!!!!

Thank you soo much... Your diagram/tree helped me out soo much.

I owe you one...

~Brandon

• I have another question now...

I know that this is kinda the opposite of what I was originally asking, but is there anyway for me to get the associative names (pretending that i don't know what they are)? Just curious...

If there's not that's fine, it's not crucial and I can work fine without it, but I was just wondering if there was anyway to do that.

Thank you again, you have saved me from countless hours of frusturation...

• var aa = [1,2,3];
aa.a="aaa";
aa.b="bbb";

var log="";
for(var i in aa){
log += ( "aa."+ i + "=" + aa[i]+ "\n" );
}

/* output:
aa.0=1
aa.1=2
aa.2=3
aa.a=aaa
aa.b=bbb */

• Thank you for your reply, I have figured out what I did wrong. Instead of:

Code:
```var a = new Array('a', 'b');
a['a'] = 'avalue';
a['b'] = 'bvalue';

/*
values in a are:
a[1] = 'a';
a[2] = 'b';
a['a'] = 'avalue';
a['b'] = 'bvalue';
*/```
I have to do this:

Code:
```var a = [];
a['a'] = 'avalue';
a['b'] = 'bvalue';

/*
values in a are:
a['a'] = 'avalue';
a['b'] = 'bvalue';
*/```
I had made a mistake at first, but I have found the answer now.

•

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•