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
8
  • Define "good". It's possible and certainly not inherently counter to any principles of ECMAScript. :-) Commented Apr 27, 2014 at 0: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... Commented Apr 27, 2014 at 0:57
  • Depends on your need, nothing specific about it. Commented Apr 27, 2014 at 0:57
  • But it is not any slower/has more cons to do this method than the multidimensional array? Commented Apr 27, 2014 at 0:59
  • @JakeChasan this is a multi-dimensional array. How else would you define one? Commented Apr 27, 2014 at 1:08

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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.

1 Comment

@AlexeiLevenkov Thanks for the note. I had never heard "jagged array" before, but I like differentiating it from "multidimensional array"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.