当我拥有数组数组时,此代码有效,但是当我输入字符串数组数组时则不行;我确实改变了代码以使用.length之后的字符串,但这有帮助。错误是“arr [i] .reduce(函数(onea,twoa){”行在第二个版本中是未定义的函数。 哦,@ user1600124,该提示可能是错误,即使我仍然输入“[[”one“,”two“,”three“],[”one1“,”two2“,”three3“]]” ,它把所有变成了一个字符串。我认为你已经解决了这个问题,但有没有提示用户输入的方法。谢谢,@ user1600124为您的解决方案!
var ArrayWidth = function(arr){
var ret = [], i;
for (i = 0; i < arr.length; i++) {
arr[i].reduce(function (onea, twoa) {
return ret[i] = Math.max(onea.length, twoa.length); //added ".length" to stings version
}, 0); //added ",0" to strings version
ret[i] = ret[i].toString().length;
}
return ret;
}
使用数字数组数组的完整代码:
//Draws Non=Jagged Table with columns be the arrays in the array of arrays and the first being the heading.
var arrays = [
[111111, 22222, 333],
[444444444, 534334, 63],
[73, 83748395, 9343],
[279571, 327894598571490581, 34815, 2]
];
function ArrayHeight (arr) {
var ret = [], i, j;
for(i = 0; i < arr.length; i++){
window["a"+i] = arr[i];
ret[i] = window["a"+i];
for(j = 0; j < ret[i].length; j++)
ret[i][j] = ret[i][j].toString();
}
return ret;
}
var ArrayWidth = function(arr){
var ret = [], i;
for (i = 0; i < arr.length; i++) {
arr[i].reduce(function (onea, twoa) {
return ret[i] = Math.max(onea, twoa);
});
ret[i] = ret[i].toString().length;
}
return ret;
}
function addSpace (){
var ret = ArrayHeight(arrays);
console.log(ret);
var widthOfRet = ArrayWidth(arrays);
console.log(widthOfRet);
var i, j, k;
for(j = 0; j < ret.length; j++){
for(k = 0; k < (ret[j].length); k++){
for(i = ret[j][k].length; i < (widthOfRet[j] + 1); i++){
ret[j][k] = ret[j][k] + " ";
}
}
}
return ret;
}
var drawTable = function(){
var ret = addSpace();
var ArrayWid = ArrayWidth(arrays);
var table = "", retFirst = "", retSecond = "", totaled = 0, i, j;
for (i = 0; i < ArrayWid.length; i++)
totaled = totaled + ArrayWid[i];
for(i = 0; i < ret.length; i++)
retFirst = retFirst + ret[i][0];
for(i = 0; i < (totaled + ret.length); i++)
retSecond = retSecond + "-";
for(j = 0; j < ret.length; j++)
for(i = 0; i < ret[j].length; i++)
window["reti"+i] = "";
for(j = 0; j < ret.length; j++)
for(i = 0; i < ret[j].length; i++)
window["reti"+i] = window["reti"+i] + ret[j][i];
table = retFirst + "\n" + retSecond;
for(i = 1; window["reti"+i] != undefined; i++)
table = table + "\n" + window["reti"+i];
return table;
}
console.log(drawTable());
不使用字符串数组:
//Draws Non-Jagged Table with columns be the arrays in the array of arrays and the first being the heading.
var arrays = prompt("To draw a table, type in an array of arrays. Each array is a column and the first is the heading. Cannot be jagged.", "[ [ , ... ], [, ... ], ... ]");
function ArrayHeight (arr) {
var ret = [], i, j;
for(i = 0; i < arr.length; i++){
window["a"+i] = arr[i];
ret[i] = window["a"+i];
for(j = 0; j < ret[i].length; j++)
ret[i][j] = ret[i][j].toString();
}
return ret;
}
var ArrayWidth = function(arr){
var ret = [], i;
for (i = 0; i < arr.length; i++) {
arr[i].reduce(function (onea, twoa) {
return ret[i] = Math.max(onea.length, twoa.length);
}, 0);
ret[i] = ret[i].toString().length;
}
return ret;
}
function addSpace (){
var ret = ArrayHeight(arrays);
console.log(ret);
var widthOfRet = ArrayWidth(arrays);
console.log(widthOfRet);
var i, j, k;
for(j = 0; j < ret.length; j++){
for(k = 0; k < (ret[j].length); k++){
for(i = ret[j][k].length; i < (widthOfRet[j] + 1); i++){
ret[j][k] = ret[j][k] + " ";
}
}
}
return ret;
}
var drawTable = function(){
var ret = addSpace();
var ArrayWid = ArrayWidth(arrays);
var table = "", retFirst = "", retSecond = "", totaled = 0, i, j;
for (i = 0; i < ArrayWid.length; i++)
totaled = totaled + ArrayWid[i];
for(i = 0; i < ret.length; i++)
retFirst = retFirst + ret[i][0];
for(i = 0; i < (totaled + ret.length); i++)
retSecond = retSecond + "-";
for(j = 0; j < ret.length; j++)
for(i = 0; i < ret[j].length; i++)
window["reti"+i] = "";
for(j = 0; j < ret.length; j++)
for(i = 0; i < ret[j].length; i++)
window["reti"+i] = window["reti"+i] + ret[j][i];
table = retFirst + "\n" + retSecond;
for(i = 1; window["reti"+i] != undefined; i++)
table = table + "\n" + window["reti"+i];
return table;
}
console.log(drawTable());
alert("Your table is in the console.log");
我的HTML:
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<script language="javascript">
//script was here
</script>
</body>
</html>
谢谢! :)
答案 0 :(得分:0)
您正在调用输入数组内容的reduce函数。 当你有数组数组时,arr [i]是一个数组,因此有reduce方法
当你有字符串数组时,arr [i]是一个字符串,并且没有你尝试使用的reduce方法。所以你最终会有错误
答案 1 :(得分:0)
试试这个:
function ArrayWidth(ary){
var r = [];
for(var i=0,l=ary.length; i<l; i++) {
r[i] = ary[i].reduce(function(a, b){
return Math.max(a, b);
}, 0);
}
return r;
}