const dimensions = [ arr.length, arr[0].length ];
This works, if you know length of the inner arrays never change.
If the dimension of the inner arrays aren't static (jagged array), you could combine Array.reduce and Math.max to calculate the largest dimension:
const dimensions = [
arr.length,
arr.reduce((x, y) => Math.max(x, y.length), 0)
];
Answer from Alex on Stack Overflowconst dimensions = [ arr.length, arr[0].length ];
This works, if you know length of the inner arrays never change.
If the dimension of the inner arrays aren't static (jagged array), you could combine Array.reduce and Math.max to calculate the largest dimension:
const dimensions = [
arr.length,
arr.reduce((x, y) => Math.max(x, y.length), 0)
];
Considering that sub lists can have different size, get the minimum size or depending on need make it max
function size(ar){
var row_count = ar.length;
var row_sizes = []
for(var i=0;i<row_count;i++){
row_sizes.push(ar[i].length)
}
return [row_count, Math.min.apply(null, row_sizes)]
}
size([[1, 1, 1], [1, 1, 1]])
Output:
[2, 3]
This recursive function returns all dimensions of a given array or false if one or more dimensions are not straight (i.e. different sizes between array items). It uses a helper function to determine whether two simple arrays are the same (read the function comments before using it).
// pre: a !== b, each item is a scalar
function array_equals(a, b)
{
return a.length === b.length && a.every(function(value, index) {
return value === b[index];
});
};
function getdim(arr)
{
if (/*!(arr instanceof Array) || */!arr.length) {
return []; // current array has no dimension
}
var dim = arr.reduce(function(result, current) {
// check each element of arr against the first element
// to make sure it has the same dimensions
return array_equals(result, getdim(current)) ? result : false;
}, getdim(arr[0]));
// dim is either false or an array
return dim && [arr.length].concat(dim);
}
console.log(getdim(123)); // []
console.log(getdim([1])); // [1]
console.log(getdim([1, 2])); // [2]
console.log(getdim([1, [2]])); // false
console.log(getdim([[1, 2], [3]])); // false
console.log(getdim([[1, 2],[1, 2]])); // [2, 2]
console.log(getdim([[1, 2],[1, 2],[1, 2]])); // [3, 2]
console.log(getdim([[[1, 2, 3],[1, 2, 4]],[[2, 1, 3],[4, 4, 6]]])); // [2, 2, 3]
console.log(getdim([[[1, 2, 3], [1, 2, 4]], [[2, 1], [4, 4]]])); // false
Run code snippetEdit code snippet Hide Results Copy to answer Expand
Here is a simple recursive function to do that. Wrote it very quickly, so may prone to errors.
This function returns the lengths of each dimensions. If it contains sub-arrays of different lengths, this function returns false.
This function works for different dimensions.
For example, passing a 2d array of 2x3 will return [2, 3], a 3d array of 2x3x4 will return [2, 3, 4]. The dimension (i.e. n d) can be obtained by checking the length of the returned array.
// Array dimension checker
// Returns:
// false when array dimensions are different
// an Array when is rectangular 0d (i.e. an object) or >=1d
function arrayDimension(a) {
// Make sure it is an array
if (a instanceof Array) {
// First element is an array
var sublength = arrayDimension(a[0]);
if (sublength === false) {
// Dimension is different
return false;
} else {
// Compare every element to make sure they are of the same dimensions
for (var i = 1; i < a.length; i++) {
var _sublength = arrayDimension(a[i]);
// HACK: compare arrays...
if (_sublength === false || sublength.join(",") != _sublength.join(",")) {
// If the dimension is different (i.e. not rectangular)
return false;
}
}
// OK now it is "rectangular" (could you call 3d "rectangular"?)
return [a.length].concat(sublength);
}
} else {
// Not an array
return [];
}
}
Demo (Check the javaScript console :) )