Problem working with multi-dimensional arrays in Javascript

In summary, the conversation discusses using HTML/Javascript to create an application that handles a large amount of data in a 3-D array structure. The person has encountered problems setting up the array and is seeking help on how to properly structure it. They receive advice on creating an array of arrays and using pointers to access different elements of the data. The conversation includes code examples and resources for creating multi-dimensional arrays in Javascript.
  • #1
aheight
321
109
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
  • #2
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
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
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:

1. What exactly is a multi-dimensional array in Javascript?

A multi-dimensional array in Javascript is an array that contains other arrays as its elements. This means that each element in the main array is also an array, creating a matrix-like structure.

2. How do I declare and initialize a multi-dimensional array in Javascript?

To declare and initialize a multi-dimensional array in Javascript, you can use the array literal notation with nested brackets. For example:
let arr = [[1,2,3], [4,5,6], [7,8,9]];
This creates a 3x3 matrix with 3 nested arrays as its elements.

3. How can I access and modify elements in a multi-dimensional array?

To access elements in a multi-dimensional array, you can use bracket notation with the index of the desired element. For example:
let arr = [[1,2,3], [4,5,6], [7,8,9]];
arr[0][1] will return the element at index 1 in the first nested array, which is 2.
To modify an element, you can simply assign a new value to the desired index using the same bracket notation.

4. What are some common problems when working with multi-dimensional arrays in Javascript?

Some common problems when working with multi-dimensional arrays in Javascript include:
- Accidentally creating a jagged array (an array where the nested arrays have different lengths)
- Forgetting to include the index of the nested array when accessing an element
- Confusion with the order of indices (remember that the first index refers to the outermost array, and the last index refers to the innermost array)

5. Are there any built-in methods for working with multi-dimensional arrays in Javascript?

Yes, there are some built-in methods that can be used for working with multi-dimensional arrays in Javascript, such as Array.prototype.map() and Array.prototype.flat(). However, these methods may not work as expected on multi-dimensional arrays, so it's important to read the documentation and test them thoroughly before using them.

Similar threads

  • Programming and Computer Science
Replies
21
Views
5K
  • Programming and Computer Science
Replies
6
Views
979
  • Programming and Computer Science
Replies
1
Views
913
  • Programming and Computer Science
7
Replies
235
Views
10K
  • Programming and Computer Science
Replies
2
Views
877
  • Programming and Computer Science
Replies
9
Views
4K
  • Programming and Computer Science
Replies
13
Views
1K
  • Programming and Computer Science
Replies
6
Views
3K
  • Computing and Technology
Replies
3
Views
2K
  • Programming and Computer Science
Replies
10
Views
2K
Back
Top