Problem working with multi-dimensional arrays in Javascript

  • Java
  • Thread starter aheight
  • Start date
  • #1
220
57

Main Question or Discussion Point

Hi,
I'm writing an HTML/Javascript application that will handle a large amount of data segregated into a set of n rings, each ring will have a set of m branches, and each branch will have four arrays of data, V, N, I, B. I would like to create a 3-D array to hold this data but am having problems setting it up. Does Javascript even handle 3D arrays?

Ideally, I would like to address for example the second ring, branch 3, data set 4 as something like

data[2][3][4].

But when I attempt to set up a simple example like below, I receive the NetBean error:
Uncaught TypeError: Cannot read property '3' of undefined

I would like to save all the data to a .js file with the following format. Can someone help me set this up?

var data[[[]]];
data[0][0][0]=[v1,v2,v3,...vn];
data[0][0][1]=[n1,n2,n3,...nn];
data[0][0][2]=[i1,i2,i3,... in];
data[0][0][3]=[b1,b2,b3,...bn];
...
data[n][m][4]=[b1,b2,b3,...bn];


HTML:
<html>
    <head>
        <title>TODO supply a title</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
    </head>
    <body>
        <div>TODO write content</div>
        <script type="text/javascript">
            var data=[[[]]];
            data[2][3][4]=24;           
            alert(data[2][3][4]);
            </script>
           
    </body>
</html>
 

Answers and Replies

  • #2
QuantumQuest
Science Advisor
Insights Author
Gold Member
926
485
JavaScript does not have a special syntax for creating multidimensional arrays - I have not fully read Ecmascript 6 specification yet, but I think there is no significant difference on this. A common solution or workaround if you will for this, is the obvious: create an array of arrays. As a hint, I can tell you to try this by forming some loops and see how it works.
 
  • #3
11,887
5,541
Here's an arrays in javascript tutorial:

http://javascript.info/tutorial/array

with multi-dim arrays example at the end. The short answer is that arrays are one dimensional in javascript however each array element can hold any datatype so to make a multi-dim array you have each element as a one-dim array

JavaScript:
var matrix = [
  [1, 2, 3],
  [4, 5, 6],
  [7, 8, 9]
]
and you can refer to a given element as matrix[1][1] where matrix[1] is [4,5,6] and matrix[1][1] is 5.
 
  • Like
Likes aheight
  • #4
220
57
Thanks a bunch! I think I have it! Looks like I need a 4D array and will have to first create the data structure in Mathematica and save it to a .js file, then during program execution, push pointers to the structure for later use.. Just have to try implementing it with real data now. :)
HTML:
<html>
    <head>
        <title>TODO supply a title</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
    </head>
    <body>
        <div>TODO write content</div>
        <script type="text/javascript">
            var data=[  
                [      
// ring 0
                 [  // branch 0
                    [1,2,3,4],   // vertex data
                    [5,6,7,8],   // normal data
                    [9,10,11,10],// indexes
                    [0,1,0,0,1,0], // barycentrics
                    []                   // load 4 pointers to data here
                 ],
                 [  // branch 1
                    [11,12,13], //  vertex
                    [14,15],    // normal
                    [16,17,18],//  indexes
                    [0,1,0,0,1],  // barycentrics
                    []
                 ]
                ],
                [
// ring 1
                 [  // branch 0
                    [1,2,3,4],   // vertex data
                    [5,6,7,8],   // normal data
                    [9,10,11,10],// indexes
                    [0,1,0,0,1,0],// barycentrics
                    []
                 ],
                 [  // branch 1
                    [11,12,13], //  vertex
                    [14,15],    // normal
                    [16,17,18],//  indexes
                    [0,1,0,0,1],  // barycentrics
                    []
                 ]
               ]
            ];
            var ring1Branch1pointers=[100,200,300,400];
// push pointers to ring1 branch1 data:
            data[0][0][4].push(1000);  //first pointer
            data[0][0][4].push(2000);  // second pointer
// access pointer for first ring, first branch, normal data
            alert(data[0][0][4][1]);
       </script>
          
    </body>
</html>
 
Last edited:

Related Threads on Problem working with multi-dimensional arrays in Javascript

Replies
6
Views
4K
Replies
4
Views
8K
Replies
3
Views
3K
Replies
3
Views
1K
Replies
4
Views
2K
  • Last Post
Replies
6
Views
2K
Replies
1
Views
2K
Replies
10
Views
2K
Replies
9
Views
3K
Replies
7
Views
4K
Top