[thelist] 2-dimensional array in jscript

Phil Turmel turmel-lap at turmel.org
Fri Sep 3 22:09:09 CDT 2004


In javascript, any array intrinsically supports arrays 
within it... "var x = []" creates an empty one-dimensional 
array, each new element of which can also be an array.  Some 
code:

var x = [];
var tmp;
while (....) // Outer Loop Conditions
   {
   tmp = [];
   while (....) // Inner Loop Conditions
     {
     tmp.push("Content")
     }
   x.push(tmp);
   }

// Read the content back out:
var e,i,j;
for (i=0; i<x.length; i++)
   for (j=0; j<x[i].length; j++)
     {
     e=x[i][j];
     // Do Something with e
     }

Hope this helps.

Phil Turmel

aspscript at canada.com wrote:

> I don't always know the what the upper bound of the
> array will be.  So since it is possible to declare a
> boundless one-dimensional array like this:
> 
> var x = []
> 
> ... I was hoping I could do the same with a two
> dimensional array. But it's not that big a deal as I
> can always just set some upper bound beyond what I am
> sure to need and go from there.  So, like your example,
> I can always do this:
> 
> var arr=[],
>   i=100;
>   do
>   arr.push([]);
>   while(0<--i);
> 
> And that will be able to handle from arr[1][1] to
> arr[100][???], which should be enough.
> 
> 
> On Thu, 2 Sep 2004 18:53:45 +0200, liorean wrote:
> 
> <snip>
> 
>>Take this example, for instance:
>>    var
>>        arr=[],
>>        i=3;
>>    do
>>        arr.push([]); // Add element to array, assign
> 
> a
> 
>>new array to it
>>    while(0<--i); // And repeat two times, generating
> 
> a
> 
>>three element array;
>>


More information about the thelist mailing list