Javascript 获取数组中维度的大小
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10237615/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get size of dimensions in array
提问by xandercoded
How to get the dimensions given a multidimensional array?
Edit: it could be of 1, 2, or 3 dimensions but each sub-array has the same length.
如何获得给定多维数组的维度?
编辑:它可以是 1、2 或 3 维,但每个子数组具有相同的长度。
i.e. for
即为
var a = [[1,1,1], [1,1,1]]
would be [2,3]
将是 [2,3]
回答by xandercoded
var dimensions = [ arr.length, arr[0].length ];
This works, given the length of the inner arrays never change.
这是有效的,因为内部数组的长度永远不会改变。
回答by Anurag Uniyal
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]
回答by Anurag Uniyal
This works for whatever dimension (supposing that each sub array has the same length):
这适用于任何维度(假设每个子数组具有相同的长度):
function getDim(a) {
var dim = [];
for (;;) {
dim.push(a.length);
if (Array.isArray(a[0])) {
a = a[0];
} else {
break;
}
}
return dim;
}
回答by SellaDev
this function will check if the array is valid (not a scalar nor a string) and if the elements of that array are valid (they have the same length) then give the dimensions if all conditions are satisfied or throw an error if else.
此函数将检查数组是否有效(不是标量或字符串),以及该数组的元素是否有效(它们具有相同的长度),然后在满足所有条件时给出维度,否则则抛出错误。
function getDim(x){
dim=[]
try {
// throw error if the passed variable is a string or a scalar
if((isFinite(x) && !x.length) || typeof(x)=='string') throw 'This is a scalar or a string not an array!';
// loop over the array to extract length of each element.
// if we get an element that is not an array, return the found dimensions
while (x){
dim.push(x.length)
currentLevel=x
x=Array.isArray(x[0])?x[0]:false;
// check if all elements of the array are of equal dimention. If not, throw an error
ans=currentLevel.every((value,index,arr)=>{ return value.length==x.length}) ;
if(!ans) throw 'elements of the array are not of equal dimension !'
}
return dim
} catch (error) {
return error
}
}
回答by ericb
var dim = [
a.length,
a[0].length
];
This should work, given that each sub array is the same length, however, if thats not the case, you might want to do something like:
鉴于每个子数组的长度相同,这应该可行,但是,如果情况并非如此,您可能想要执行以下操作:
function findDim(a){
var mainLen = 0;
var subLen = 0;
mainLen = a.length;
for(var i=0; i < mainLen; i++){
var len = a[i].length;
subLen = (len > subLen ? len : subLen);
}
return [mainLen, subLen];
};
回答by Tronix117
var a = [[1,1,1], [1,1,1]];
var size=[];
while(s=a.pop) size.push(s.length);
Or if you want to have the length inside a
:
或者如果你想在里面有长度a
:
var a = [[1,1,1], [1,1,1]];
for(i in a) a[i]=a[i].length;
Edit: I apologize, I wasn't in the subject. The following code calculate max row and column for a two dimensional array.
编辑:我很抱歉,我不在这个主题中。以下代码计算二维数组的最大行和列。
var innerSize = 0, i=0, l=a.length, l2;
for(;i<l;i++) if(innerSize<(l2=a[i].length)) innerSize = l2
[l, innerSize]
You can change the <
to >
if you want the minimum size.
您可以更改<
到>
,如果你想的最小尺寸。