Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript Inner-Array

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
like image 578
Jake Chasan Avatar asked Apr 27 '14 00:04

Jake Chasan


People also ask

How do I access an inner array?

To access an element of the multidimensional array, you first use square brackets to access an element of the outer array that returns an inner array; and then use another square bracket to access the element of the inner array.

How do nested arrays work in JavaScript?

After creating a JavaScript nested array, you can use the “push()” and “splice()” method for adding elements, “for loop” and “forEach()” method to iterate over the elements of the inner arrays, “flat()” method for reducing the dimensionality, and “pop()” method to delete sub-arrays or their elements from the nested ...

Can you have an array within an array JavaScript?

JavaScript lets us create arrays inside array called Nested Arrays. Nested Arrays have one or many arrays as the element of an array.

What is outer and inner array?

The design for the control factors is called the inner array, and the design for noise factors is called the outer array. The outer array is replicated for each of the runs in the inner array, and a performance measure ("signal-to-noise ratio") is computed over the replicate.


2 Answers

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.

like image 182
Alexei Levenkov Avatar answered Sep 29 '22 00:09

Alexei Levenkov


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.

like image 28
Oriol Avatar answered Sep 28 '22 23:09

Oriol