我写了一个函数,它为[“rows”+ i]赋值,并且知道我设置值的每一个都有一个变量。我无法弄清楚为什么,当我取每个的长度并将每个作为唯一索引放在先前空的数组中时,其值为undefined。
我的错误将出现在代码的这一部分;我已经找到了这个bug,但还没有找到它(这不是整个代码,只是我认为该bug的部分:
var aLength = [];
for (var i = 0; i < arrays.length; i++)
aLength[i] = window["rows2"+i].length;
return aLength;
} // this is where my ends
console.log(width(arrays));
// [未定义,未定义,未定义,未定义]
完整代码:
var arrays = [[111111, 22222, 333], [444444444, 534334, 63], [73, 83748395, 9343], [279571, 327894598571490581, 34815, 2]];
var width = function(arrayOfArrays){
var a = arrays.length;
for(var i = 0; i < arrays.length; i++)
window["array"+i] = arrays[i];
for(var i = 0; i < arrays.length; i++){
window["rows"+i] = window["array"+i];
window["rows"+i].reduce(function (onea, twoa){
return window["rows2"+i] = Math.max(onea, twoa);
}, []);
console.log(window["rows2"+i]);
}
var aLength = [];
for (var i = 0; i < arrays.length; i++)
aLength[i] = window["rows2"+i].length;
return aLength;
}
console.log(width(arrays));
//111111
//444444444
//83748395
//327894598571490560
//[undefined, undefined, undefined, undefined]
谢谢! :)