我怎样才能得到一个多维数组的Javascript的尺寸是多少? [英] How can I get the dimensions of a multidimensional Javascript array?
问题描述
通常情况下,在有些情况下,我需要确定一个Javascript阵列是矩形(并获得数组的大小)的情况。在这种情况下,我的意思是确定该数组的每个元素是否具有相同的长度的数组。我怎样才能做到这一点?
函数getArrayDimensions(theArray){
//如果阵列的尺寸是3×3,返回[3,3],以及用于任何尺寸的阵列做同样的
//如果数组不是矩形,返回false
}
此外,如何能这一功能推广到多维数组(2x5x7,3x7x8x8,等等)?
这递归函数返回或给定数组的所有维度假,如果一个或多个维度都直不(数组项之间即不同尺寸)。它使用一个辅助函数来确定两个简单数组是否相同(阅读使用之前功能评论)
// pre:A == B,每个项目是一个纯量\r
功能array_equals(A,B)\r
{\r
返回则为a.length === b.length个和放大器;&安培; a.every(功能(价值指数){\r
返回值=== B〔指数]\r
});\r
};\r
\r
功能getdim(ARR)\r
{\r
如果(/ *!(ARR的instanceof阵列)|| * /!arr.length){\r
返回[]; //当前阵列无量纲\r
}\r
VAR昏暗= arr.reduce(功能(结果,电流){\r
//检查ARR的每个元素对的第一个元素\r
//确保它具有相同的尺寸\r
返回array_equals(结果,getdim(电流))?结果:假的;\r
},getdim(ARR [0]));\r
\r
//暗淡或者是虚假或数组\r
返回暗淡和放大器;&安培; [arr.length] .concat(DIM);\r
}\r
\r
的console.log(getdim(123)); // []\r
的console.log(getdim([1])); // [1]\r
的console.log(getdim([1,2])); // [2]\r
的console.log(getdim([1,[2]])); //假\r
的console.log(getdim([1,2],[3])); //假\r
的console.log(getdim([1,2],[1,2]])); // [2,2]\r
的console.log(getdim([1,2],[1,2],[1,2]])); // [3,2]\r
\r
的console.log(getdim([[[1,2,3],[1,2,4]],[[2,1,3],[4,4,6]]])); // [2,2,3]\r
\r
的console.log(getdim([[[1,2,3],[1,2,4]],[[2,1],[4,4]]])); //假
\r
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
这篇关于我怎样才能得到一个多维数组的Javascript的尺寸是多少?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!