PDA

View Full Version : 2d array help



brlslngr
Nov 15th, 2009, 04:34 PM
Im trying to create a 2d array with these two rows -

Example1, Example2, Example3
Example4, Example5, Example6

To print I use this for loop-

for(i = 0; i < 2; i++)
{
for( j = 0 ; j < 3; j++)
{
document.write(array[i][j]);
}
}

I dont Understand how to create the actual 2d array though. Can someone please shed some light on the situation. Thanks alot :)

vwphillips
Nov 15th, 2009, 04:53 PM
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>
<title></title>
</head>

<body>
<script type="text/javascript">

var array=[]
array[0]=['Example1', 'Example2', 'Example3'];
array[1]=['Example4', 'Example5', 'Example6'];

for(var a,i = 0; i < array.length; i++){
a=array[i];
for (var j = 0 ; j < a.length; j++){
document.write(array[i][j]);
}
}

</script>
</body>

</html>

brlslngr
Nov 15th, 2009, 05:11 PM
that just prints them on 1 row though

vwphillips
Nov 15th, 2009, 05:27 PM
document.write(array[i][j]+'<br>');

brlslngr
Nov 15th, 2009, 05:29 PM
that would just make it a verticle row.

vwphillips
Nov 15th, 2009, 05:38 PM
for(var a,i = 0; i < array.length; i++){
a=array[i];
for (var j = 0 ; j < a.length; j++){
document.write(array[i][j]);
}
document.write('<br>');
}