Java Problem working with multi-dimensional arrays in Javascript

  • Thread starter Thread starter aheight
  • Start date Start date
  • Tags Tags
    Arrays Javascript
Click For Summary
The discussion centers on creating a multidimensional array in JavaScript to manage a complex data structure consisting of rings, branches, and associated data arrays. The user initially encounters issues when trying to set up a 3D array, receiving an error related to accessing undefined properties. It is clarified that JavaScript does not have a built-in syntax for multidimensional arrays; instead, it allows the creation of arrays of arrays. A common approach is to initialize a 3D array by nesting arrays within arrays. The user later realizes the need for a 4D array and considers using Mathematica to structure the data before saving it to a JavaScript file. The conversation includes examples of how to structure the data and access specific elements, emphasizing the flexibility of JavaScript arrays to hold various data types. Ultimately, the user finds a solution to implement the data structure effectively.
aheight
Messages
318
Reaction score
108
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>
 
Technology news on Phys.org
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.
 
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
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:
Learn If you want to write code for Python Machine learning, AI Statistics/data analysis Scientific research Web application servers Some microcontrollers JavaScript/Node JS/TypeScript Web sites Web application servers C# Games (Unity) Consumer applications (Windows) Business applications C++ Games (Unreal Engine) Operating systems, device drivers Microcontrollers/embedded systems Consumer applications (Linux) Some more tips: Do not learn C++ (or any other dialect of C) as a...

Similar threads

  • · Replies 21 ·
Replies
21
Views
6K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
Replies
235
Views
14K
  • · Replies 13 ·
Replies
13
Views
2K
  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 9 ·
Replies
9
Views
4K
  • · Replies 6 ·
Replies
6
Views
3K