...

View Full Version : Multidimensional array element overwriting



bourbonmonkey
02-20-2007, 06:36 AM
Hey,

Im new to javascript but i'm an experienced programmer, im having a problem with a 2 dimensional array where assigning to additional elements overwrites all elements. I suspect theres an easy fix or that im just missing a language fundamental. Heres the source code of the problem as simple as i could make it. cut paste into html and it works. Any help would be appreciated.



<script>

var creditAmounts = new Array();

for (i=0;i<2;i++)
{
alert("I :: " + i);
creditAmounts[i] = new Array();
creditAmounts[i,0] = "creditAmount"+i;
creditAmounts[i,1] = i;

}
document.write( "[0][0]: " + creditAmounts[0,0] + "<br>");
document.write( "[0][1]: " + creditAmounts[0,1] + "<br>");
document.write( "[1][0]: " + creditAmounts[1,0] + "<br>");
document.write( "[1][1]: " + creditAmounts[1,1] + "<br>");
</script>



Running the script results in [0][0] and [1][0] having the same value...

vwphillips
02-20-2007, 08:08 AM
var creditAmounts = new Array();


for (i=0;i<2;i++){
creditAmounts[i] = new Array();
creditAmounts[i][0] = "creditAmount"+i;
creditAmounts[i][1] = i;

}

or

var creditAmounts = [];

for (i=0;i<2;i++){
creditAmounts[i]= ["creditAmount"+i,i];
}

bourbonmonkey
02-20-2007, 09:11 AM
Hey thanks,

I really should have double checked the syntax on that one, i was just referencing from another post i saw. That'll teach me. :P



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum