Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Looping Through A Multi Dimensional Associative Array

Status
Not open for further replies.

Reesy

Programmer
Aug 7, 2002
60
GB
As the subject title says, I want to know how to loop through a multi dimensional associative array.

With a 1 dimensional array I know it is...
Code:
myArray = new Array();
myArray['test_a'] = 'first val';
myArray['test_b'] = 'second val';

for (varIndex in myArray)
{
  alert(myArray[varIndex]);
}

If however I have an array ...

myArray = newArray(new Array(),new Array());

How then would I do a similar thing to above i.e. loop through each array element?

Cheers, Reesy
Blood, Sweat But No Tears!
 
Nested for loops!!!

myArray = newArray(new Array(),new Array());
for (varIndex in myArray)
{
alert(myArray[varIndex]);
for (varIndex2 in myArray)
{
alert(myArray[varIndex][varIndex2]);
}
}

Hope this helps!

Cheers!
Laura
 
Thanks for that!
I'm still having a problem though...
I'm expecting the following code to alert once with the text 'test1 test2'
but it alerts 9 times with the text...
0 0
0 1
0 test2
1 0
1 1
1 test2
test2 0
test2 1
test2 test2

Code:
myArray = new Array(new Array(), new Array());
myArray['test1','test2'] = 'This is a test';

for (varIndex in myArray)
{
  for (varIndex2 in myArray)
  {
   alert(varIndex+' '+varIndex2);
  }
}

What am I doing wrong? [ponder] Blood, Sweat But No Tears!
 
hi reesy,
don't nest the loops:

<script language=&quot;javascript&quot;>
arrayOne=[&quot;array one/element one&quot;,&quot;array one/element two&quot;];
arrayTwo=[&quot;array two/element one&quot;,&quot;array two/element two&quot;];
for(index1 in arrayOne){
document.write(arrayOne[index1]+&quot;<br>&quot;);
}
for(index2 in arrayTwo){
document.write(arrayTwo[index2]+&quot;<br>&quot;);
}
</script>

sundemon
 
Thanks Sundemon
but that's not quite the same thing unless I've completely missed the point
(which has been known to happen!) [rednose]

I am using associative arrays so that I can directly reference values easily as well as loop through them but the &quot;for (index in array)&quot; seems to bring back a 0 and a 1 aswell as the key indexes.

Cheers, Reesy Blood, Sweat But No Tears!
 
Define a function that shows an array --

Code:
function showArray(arr) {
  var tS = new String()
  for (var iI in arr) {
    tS += &quot;Index &quot;+iI+&quot;, Type &quot;+(typeof arr[iI])+&quot;, Value &quot;+arr[iI]+&quot;\n&quot;
    }
  return tS
}

Pass
Code:
showArray
a simple array, it returns a list of indices, types, values. Pass an array of associative arrays, it returns a list of indices, type -- object, no values. To list the contents of an array of arrays --

Code:
function walkArray(arr) {
  var tS = new String()
  for (var iI in arr) {
    tS += &quot;Index &quot;+iI+&quot;, Type &quot;+(typeof arr[iI])+
          &quot;, Value -- \n&quot;+showArray(arr[iI])+&quot;\n&quot;
    }
  return tS
}

Works for simple and associative arrays of arrays. See example at
 
Reesy to understand Multidimensional arrays you have to understand a few things about the normal arrays. Multidimensional arrays are nothing more than arrays within arrays.

Here is a shorthand way of defining an array :

var numbers = [1, 2, 3, 4]

It is the same as doing this :

var numbers = new Array()
numbers[0] = 1
numbers[1] = 2
numbers[2] = 3
numbers[3] = 4

In both examples to get the value at index 2 I would need to write numbers[2] and I would get three. But notice that doing this : [1, 2, 3, 4][2] also gives me three.

You can also have arrays within an array like so :

var numbers = new Array()
numbers[0] = [11, 22, 33, 44]
numbers[1] = [22, 564, 987, 456]
numbers[2] = [3]
numbers[3] = [4, 8]

And the shorthand version would be :

var numbers = [ [11, 22, 33, 44] ,[22, 564, 987, 456], [3], [4, 8] ]

This is a multidimensional array. And to loop through it is actually quite easy. First I will give you an example of how I can get the elements of a normal array :

var numbers = [1, 2, 3, 4]
for (var index = 0; index < numbers.length; index++)
{
document.write(&quot;<br> at index : &quot; + index + &quot; the value is &quot; + numbers[index])
}

This would write down on your page :

at index 0 the value is 1
at index 1 the value is 2
at index 2 the value is 3
at index 3 the value is 4

Now if you had a multidimensional array you could also print things out like that :

var numbers = [ [11, 22, 33, 44] ,[22, 564, 987, 456], [3], [4, 8] ];
for (var index = 0; index < numbers.length; index++)
{
document.write(&quot;<br> at index : &quot; + index + &quot; the value is &quot; + numbers[index])
}

The result on screen would be :

at index : 0 the value is 11,22,33,44
at index : 1 the value is 22,564,987,456
at index : 2 the value is 3
at index : 3 the value is 4,8

Now if you see the content at each index is actually an array. When an array is printed or alerted javascript transforms it into a string seperating each element by a coma.

Since each element at each index is an array you simply have to nest another for loop and loop through each array at every index :

var numbers = [ [11, 22, 33, 44] ,[22, 564, 987, 456], [3], [4, 8] ];
for (var index = 0; index < numbers.length; index++)
{
for (var innerIndex = 0; innerIndex < numbers[index].length; innerIndex++)
{
document.write(&quot;<br> at [index][innerIndex] : [&quot; + index + &quot;][&quot; + innerIndex + &quot;]the value is &quot; + numbers[index][innerIndex])
}
}

at [index][innerIndex] : [0][0]the value is 11
at [index][innerIndex] : [0][1]the value is 22
at [index][innerIndex] : [0][2]the value is 33
at [index][innerIndex] : [0][3]the value is 44
at [index][innerIndex] : [1][0]the value is 22
at [index][innerIndex] : [1][1]the value is 564
at [index][innerIndex] : [1][2]the value is 987
at [index][innerIndex] : [1][3]the value is 456
at [index][innerIndex] : [2][0]the value is 3
at [index][innerIndex] : [3][0]the value is 4
at [index][innerIndex] : [3][1]the value is 8

I hope this clears things out. Gary Haran
 
Speaking of missing the point, I just did with my previous response. The response addresses arrays of arrays, the question asks about multi-dimensional arrays. Doh!

Javascript doesn't support multi-dimensional arrays. You can use arrays of arrays to simulate multi-dimensional arrays of both the simple and associative variety --
Native JavaScript control statements (like
Code:
for/in
) are only going to see one level of the multi-level simulation. You'll have to provide some help for the native methods to work. There's a bunch of alternatives on this page.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top