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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Javascript multi-dimensional array syntax

Status
Not open for further replies.

lazyRascal

Programmer
Feb 27, 2006
22
0
0
US
I'm trying to create a three dimension array in javascript. I know since javascript doesn't support multi-dimensional arrays, as such, that to accomplish equavalent results you must create arrays of arrays.

I thought the following code would work.

var matrix = new Array(4)
for (i=0; i <10; i++)
var matrix=new Array(10)
for (i=0; i<2; i++)
var matrix=new Array(3)

I'm want to create an array whose last value could be referenced by matrix[2][9][3].

Any guidence is appreciated.

Thanks
 
You're kinda on the right track - here's what you need to change:
Code:
var matrix = new Array(4)
for (i = 0; i < 10; i++) {
   [s]var[/s] matrix[i] = new Array(10)
   for ([!]j[/!] = 0; [!]j[/!] < 2; [!]j[/!]++) {
      [s]var[/s] matrix[i][!][j][/!] = new Array(3)
   }
}

-kaht

[small] <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <.</B>[/small]
[banghead] [small](He's back)[/small]
 
Thank you for the responses.

What I'm trying to do is persist the results of a recordset where I can manipulate it with javascript. Here is the entire code of what I'm testing. Would someone give it a 'look see' of the javascript portion for obvious syntax errors.

My objective is to produce an array with 3 pages of 10 rows of 4 fields.

Code:
<html>
<head>
<script language="JavaScript">
var matrix = new Array(4)
for (i = 0; i < 10; i++) {
   matrix[i] = new Array(10)
   for (j = 0; j < 2; j++) {
      matrix[i][j] = new Array(3)
   }
}
</script>
</head>

<body>

<%
set conn=Server.CreateObject("ADODB.Connection")
conn.Provider=("Microsoft.Jet.OLEDB.4.0")
conn.Open(Server.Mappath("myTests.mdb"))
set rs = Server.CreateObject("ADODB.recordset")
rs.CursorLocation = 3
rs.Open "Select fName, lName, company, phone from Contacts", conn

nItemsPerPage = 10
RS.PageSize = nItemsPerPage
nPageCount = RS.PageCount
nPage = CLng(Request.QueryString("Page"))
If nPage < 1 Then
	nPage = nPageCount
End If
If nPage > nPageCount Then
	nPage = 1
End If
RS.AbsolutePage = nPage

dim vbPage
vbPage = 0
dim vbRow
vbRow = 0

For cv = 1 To 30
%>

<script>
  matrix[<%=vbPage%>][<%=vbRow%>][0] = "<%=RS("fName")%>";
  matrix[<%=vbPage%>][<%=vbRow%>][1] = "<%=RS("lName")%>";
  matrix[<%=vbPage%>][<%=vbRow%>][2] = "<%=RS("company")%>";
  matrix[<%=vbPage%>][<%=vbRow%>][3] = "<%=RS("phone")%>";
</script>

<%
vbRow = vbRow + 1
If (vbRow = 10) Then
  vbPage = vbPage + 1
  vbRow = 0
End If

Next

rs.close
conn.close
%>


</body>
</html>


The IE javascript debugger is indicating:

Error 'matrix.2.2' is null or not an object... for all of [matrix.2.2 through matrix.2.9] for a total of eight errors, which makes me think it's a array subscript problem. My intention is to dimension an array of 3x10x4. My concern is that I'm actually declaring an array 4x10x3.

btw, is there any freeware tool for debugging javascript?
 
There are probably many good free tools for JavaScript debugging, but the two I use are Microsoft's Script Debugger (for IE), and the Venkman debugger for Firefox.

I'm a bit confused as you why you're defining your top level array with "new Array(4)" when you only need it to have 3 entries. Is there a reason for this?

Dan



[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
It's likely the confusion about the array is on my part.

I may be totally wrong, but what I thought I was doing is declaring a 4 element array referenced as matrix[0]...matrix[3].

Then the first loop is declaring an array of 10 arrays of these preceeding arrays referenced as matrix[0][0]...matrix[9][3].

The second loop is declaring an array of 3 elements of the preceeding which would be referenced matrix[0][0][0]...matrix[2][9][3].

The declartion values are base 1 and the subscripts are base 0.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top