Grateful
Grateful

Reputation: 10175

Is my understanding of multi-dimensional arrays in javascript correct?

I think of arrays as follows.

var array1D = [];                            // 1D Array
var array2D = [ [], ... ];                   // 2D Array
var array3D = [ [ [], ... ], ... ];          // 3D Array

So the following are all examples of a 2D array.

var x = [ [] ];                              // 2D, not 1D!
var y = [ [], [] ];                          // 2D, as expected
var z = [ [], [], [] ];                      // 2D, not 3D!

Likewise, the following are all examples of a 3D array.

var x = [ [ [] ] ];                          // 3D, not 1D!
var y = [ [ [] ], [ [] ] ];                  // 3D, not 2D!
var z = [ [ [] ], [ [] ], [ [] ] ];          // 3D, as expected

Upvotes: 0

Views: 43

Answers (3)

Chris Lear
Chris Lear

Reputation: 6742

The answer to the question in the title appears to be "yes". [Added as an answer here only because @Grateful offered me points - I'm that shallow]

Upvotes: 0

czl
czl

Reputation: 117

In javascript, array can be built in difference structure. except the standard multi-dimensional, It can also like belows:

 [{},[[]],[]]
 [[[]],[]]

Upvotes: 0

Álvaro González
Álvaro González

Reputation: 146460

In fact, multi-dimensional arrays don't even exist.

You only have single-dimension arrays. However, since an array can contain elements of any type, you have the possibility of using arrays as items. That's good enough to simulate matrices of any arbitrary dimension.

Upvotes: 1

Related Questions