Problem working with multi-dimensional arrays in Javascript

  • Context: Java 
  • Thread starter Thread starter aheight
  • Start date Start date
  • Tags Tags
    Arrays Javascript
Click For Summary

Discussion Overview

The discussion revolves around creating and managing multi-dimensional arrays in JavaScript, specifically for an application handling data organized into rings and branches. Participants explore the feasibility of implementing a 3D or 4D array structure to store various datasets.

Discussion Character

  • Technical explanation
  • Exploratory
  • Debate/contested

Main Points Raised

  • One participant questions whether JavaScript can handle 3D arrays and provides an example of how they intend to structure their data.
  • Another participant notes that JavaScript does not have a special syntax for multidimensional arrays and suggests creating an array of arrays as a workaround.
  • A third participant provides a tutorial link and explains that while arrays in JavaScript are one-dimensional, each element can hold another array, effectively creating a multi-dimensional structure.
  • A later reply indicates a shift in approach, suggesting the need for a 4D array and proposing to create the data structure in Mathematica before saving it to a JavaScript file for use.
  • This participant also shares a detailed example of a nested array structure that includes pointers for later use.

Areas of Agreement / Disagreement

Participants do not reach a consensus on the optimal structure for the data, with differing opinions on whether a 3D or 4D array is necessary. The discussion remains unresolved regarding the best approach to implement the required data structure.

Contextual Notes

Some participants express uncertainty about the limitations of JavaScript's array handling capabilities and the implications of using nested arrays. There are also unresolved questions about the efficiency and practicality of the proposed solutions.

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   Reactions: 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:

Similar threads

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