Often, there are situations where I need to determine whether a Javascript array is rectangular (and get the dimensions of the array). In this case, I mean determining whether every element of the array is an array with the same length. How can I do this?
function getArrayDimensions(theArray){
//if the array's dimensions are 3x3, return [3, 3], and do the same for arrays of any dimension
//if the array is not rectangular, return false
}
Additionally, how can this function be generalized to multidimensional arrays (2x5x7, 3x7x8x8, etc.)?
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With