5

Is it good practice to create a JavaScript array inside another array? Example: I would create an array, "array1", which would contain objects of "array2" and "array3".

Example Code:

var array1 = [];
var array2 = []
array1.push(array2);

The reason I ask this question, is that with a multidimensional array, it seems hard to get the length of all dimensions, however, with this "inner-array," you could get the dimensions quite easily:

array1[0].length
Jake Chasan
  • 6,290
  • 9
  • 44
  • 90
  • Define "good". It's possible and certainly not inherently counter to any principles of ECMAScript. :-) – RobG Apr 27 '14 at 00:57
  • Not an answer, just a comment: I'd see no issue with this. I routinely use Arrays-within-Arrays in PHP, because it is a much more logical manner of accessing nested data than attempting to use traditional multi-d arrays, which end up with me having to draw a little diagram on a post-it note to remember where all my data is being stored... – Singular1ty Apr 27 '14 at 00:57
  • Depends on your need, nothing specific about it. – The Alpha Apr 27 '14 at 00:57
  • But it is not any slower/has more cons to do this method than the multidimensional array? – Jake Chasan Apr 27 '14 at 00:59
  • @JakeChasan this _is_ a multi-dimensional array. How else would you define one? – Benjamin Gruenbaum Apr 27 '14 at 01:08
  • I don't believe that this is a traditional multidimensional array, because each element can have a variable length. I would typically define a 2-dimensional array (in pseudocode) as array[][] making the shape rectangular. I guess in JavaScript, I would define one as var array = [[]], which would be the same as what I was trying to accomplish with this question. Does anyone know why JavaScript does not allow multidimensional array definitions like other languages, specifically C or Java? – Jake Chasan Apr 27 '14 at 01:14
  • @BenjaminGruenbaum A single `Array` instance that alone has multiple dimensions, though JavaScript doesn't have support for such a type. – Jonathan Lonowski Apr 27 '14 at 01:14
  • @JakeChasan - no, there is no "true" multidimensional arrays in JavaScript (see my answer - JS supports only jagged arrays, which is fine to use as real multidimensional rectangular array as long as you are careful with setting it up) – Alexei Levenkov Apr 27 '14 at 01:22

2 Answers2

3

Note that generally it is called "jagged array" (array of arrays) which is not necessary represent rectangular matrix (usually multidimensional arrays that have same number of elements on each level).

JavaScript does not support what usually considered "multidimensional arrays" unlike some other languages where you can do something like matrix = new int[10,20]. So this is the only way to create "multidimensional array" in JavaScript. This post How to initialize a jagged array in JavaScript? may give you other way to initialize the array.

If using it for matrix manipulation code be careful when adding inner arrays: usually code would expect rectangular shape(like nested for loops) and may fail if some inner arrays have wrong dimensions.

Community
  • 1
  • 1
Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
2

Is it good practice to create a JavaScript array inside another array?

The elements contained in an array can be everything, even other arrays. So there's nothing bad in doing that.

The reason I ask this question, is that with a multidimensional array, it seems hard to get the length of all dimensions, however, with this "inner-array," you could get the dimensions quite easily

Your "inner-array" is, in fact, a multidimensional array.

Oriol
  • 274,082
  • 63
  • 437
  • 513
  • @AlexeiLevenkov Thanks for the note. I had never heard "jagged array" before, but I like differentiating it from "multidimensional array" – Oriol Apr 27 '14 at 01:31