1. ## Confusing multi-dimensional array

var myMultiArray = [
[1,2,3,4,5, [1,2,3,4,5] ],
[6,7,8,9,10 , [1,2,3,4,6] ],
[11,12,13,14,15 , [1,2,3,4,5] ],
[16,17,18,19,20, [1,2,3,4,5] ]
];

console.log( myMultiArray[1][5][4] ); //Outputs 6 , the value in the last element of the last element of the second element of myMultiArray.

what does 1 represent?
what does 5 represent?
what does 4 represent?

In a 2d multi dimensional array, the first index is row, the second index is column.

so this one is confusing me, since it has more than 2 indexes.

2. very simple. Try one index at a time

3. interesting approach, thanks.

4. The main array has 4 members, indexed 0-3, each an array
Each of those arrays has 6 members, indexed 0-5, the first 5 members being Ints and the last an array
Each of the last arrays have 5 members, indexed 0-4, Ints

The drill down [1][5][4] goes to
second member -> sixth member -> fifth member

5. ## The only thing left

Originally Posted by Mittineague
The main array has 4 members, indexed 0-3, each an array
Each of those arrays has 6 members, indexed 0-5, the first 5 members being Ints and the last an array
Each of the last arrays have 5 members, indexed 0-4, Ints

The drill down [1][5][4] goes to
second member -> sixth member -> fifth member

the other question i have is why nest an array within each array? what possible use case scenarios are there for this?

I can't really visualize a scenario where i would use something this weird?

6. JavaScript does not support multi-dimensional arrays so the only way to create arrays with two or more dimensions is to nest arrays insode of arrays. When it comes to where you have data for a two dimensional array you would ususally set up an array for the rows and nest an array giving the values for each column inside of each row element as that makes it easiest to translate into HTML.

7. Originally Posted by felgall
JavaScript does not support multi-dimensional arrays so the only way to create arrays with two or more dimensions is to nest arrays insode of arrays. When it comes to where you have data for a two dimensional array you would ususally set up an array for the rows and nest an array giving the values for each column inside of each row element as that makes it easiest to translate into HTML.
Can you please provide a simplified example where each row is a baseball player for example and each column is a stat like batting average, steals, and strikeouts for example?

And i was also wondering how would multi-dimensional arrays be implemented in a regular oop language like java or C++ for example? since you say that javascript doesn't support it, i want to see the difference.

8. Originally Posted by wc43870n
since you say that javascript doesn't support it, i want to see the difference.
In languages that support multi dimensional arrays you would reference the elements of a three dimensional array using something like myArray[2,4,6] whereas in languages that don't support multi dimensional arays where you have to nest arrays inside of other arrays to achieve the same result you'd reference it like myArray[2][4][6]. The way you define the arrays would be different as well since in languages that support multi dimensional arrays you usually need to spepecify how many entries there are in each dimension.

With nested arrays you don't have to have the same number of entries in each dimension the way you do with multi domensional arrays. So in JavaScript with your array of baseball players there is nothing in the language that requires that you have a second array inside of each that has four entries - they can have any number.

With the baseball example I'd actually be more inclined to use a single array where each entry is an object with four named properties:

players = [{player:'joe',average:10,steals:28,striheouts:12},
{player:'sam',average:12,steals:17,strikeouts:10},
...
]

Now players[0].player = 'joe, players[1].steals = 10 etc - far easier to tell what is what rather than having to remember that [0] is the name, etc.

9. Code:
```players = [{player:'joe',average:10,steals:28,striheouts:12},
{player:'sam',average:12,steals:17,strikeouts:10},
...
]```
so this is how its done in java?

so in java, you would access a 3 dimensional array like this:

myArray[2,4,6].

But how would you define the 3 dimensional array and could we use baseball as the example for defining it?

Code:
```players = [{player:'joe',average:10,steals:28,striheouts:12},
{player:'sam',average:12,steals:17,strikeouts:10},```
the above example is only 1 dimensional? correct?

10. Originally Posted by wc43870n
And i was also wondering how would multidimensional arrays be implemented in a regular oop language like java or C++ for example? since you say that javascript doesn't support it, i want to see the difference.
Java doesn't support multidimensional arrays either. myArray[2, 4, 6] is syntax sugar for arrays of arrays, like in JavaScript. BTW, what "regular OOP" got to do with this?

Multidimensional arrays are only attractive for math intensive work, if there is also a contigous memory allocation for the array elements. Java and JavaScript don't offer this. Are you looking for this? Then go with C or other languages that allow you direct memory access.

If you want to store structured data, try the JSON format. If you want to keep using arrays for this data, I suggest you look up stacked arrays. It involves using a formula to calculate element's indices. You have to come up with the formula for your custom arrays of arrays construction, which is but one linear array.

11. Originally Posted by myty
Java doesn't support multidimensional arrays either. myArray[2, 4, 6] is syntax sugar for arrays of arrays, like in JavaScript. BTW, what "regular OOP" got to do with this?

Multidimensional arrays are only attractive for math intensive work, if there is also a contigous memory allocation for the array elements. Java and JavaScript don't offer this. Are you looking for this? Then go with C or other languages that allow you direct memory access.

If you want to store structured data, try the JSON format. If you want to keep using arrays for this data, I suggest you look up stacked arrays. It involves using a formula to calculate element's indices. You have to come up with the formula for your custom arrays of arrays construction, which is but one linear array.
just wanted to know more about regular oop languages for context and also comparison.

what languages do support multi dimensional arrays.

12. OK, to clarify the OO thing. The strict definition of object oriented revolves around one thing: the object.

What is an object? An object is a collection of name-value pairs properties. Some of the pairs may define data, some of the pairs may describe behavior.

With this definition, JavaScript is certainly one of the regular oop languages.

Things start to get blurry and arguments tend to heat up with more advance topics like inheritance, encapsulation, polymorphism. While there is a single theoretical ground for these techniques, implementations may vary, leading to endless talks. So lets end this here before it turns in another flame war.

Your last question, in a way, is similar to the oop debate. What languages support multidimensional arrays is not important right now. They all do, but implementations may very. Instead, stop making us take shots in the dark and clarify your goal.

Is your goal to write a powerful numbers crunching library? Then JavaScript is not for you. Neither are JavaScript arrays any help here, implicitly.

Is your goal structured data representation and manipulation? Then JavaScript may be the one to help. What other requirements do you have, that is the question?

My point is, there's no general answer to an unspecific question. Performance, code readability, maintenance, target platform are only a few factors that could help you and us shape a useful answer. Which is usually a pattern, but being more specific stops you from choosing a slightly inappropriate one.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•