我正在尝试为Barmar帮助我在part one创建的功能添加更多功能。基本上我正在创建一个多维数组,它将从开始和结束数字中计算出数字范围。对我来说有点棘手的是,子范围需要嵌套在之前的范围内。这用aInput数组位置的第一项表示。例如,以下输入数组将生成一个长度为30行,每行3列的新数组。
var aInput = new Array();
aInput[0] = new Array("0", "1", "5");
aInput[1] = new Array("1", "1", "3");
aInput[2] = new Array("2", "1", "2");
输出看起来像这样:
0: Array[3]
1: Array[3]
2: Array[3]
3: Array[3]
4: Array[3]
5: Array[3]
6: Array[3]
7: Array[3]
0: 2
1: 1
2: 2
length: 3
8: Array[3]
9: Array[3]
10: Array[3]
11: Array[3]
12: Array[3]
13: Array[3]
14: Array[3]
15: Array[3]
16: Array[3]
17: Array[3]
18: Array[3]
19: Array[3]
20: Array[3]
21: Array[3]
22: Array[3]
23: Array[3]
24: Array[3]
25: Array[3]
26: Array[3]
27: Array[3]
28: Array[3]
29: Array[3]
length: 30
正如您所看到的,我已经扩展了第7行,它内部有3个项目(列)。完整数组从1-5开始计数,其中嵌套的rage在计算输入数组1-3的第二行,并且其中一个具有另一个嵌套范围,计数为1-2。简单的概念,但有点难以解释。基本上我们正在创建一个数字网格。感谢Barmar这一切都很好。新问题是我需要创建多个数字网格并将它们叠加在一起。这意味着我的输入数组看起来像这样。
aInput[0] = new Array("0", "1", "5");
aInput[1] = new Array("1", "1", "3");
aInput[2] = new Array("0", "10", "15");
aInput[3] = new Array("1", "10", "12");
结果应该是一个长度为42和两列的数组。我知道这应该如何运作。我需要为集合添加另一个循环。我在input_indexed数组中定义,但我有一些问题,了解推送如何在多维数组上工作。这就是我到目前为止所拥有的。任何帮助是极大的赞赏。
// JavaScript Document
var aInput = new Array();
aInput[0] = new Array("0", "1", "5");
aInput[1] = new Array("1", "1", "3");
aInput[0] = new Array("0", "10", "12");
aInput[1] = new Array("1", "40", "41");
var input_indexed = [],
elem = [];
var rObject = {};
var set = -1;
// Get aInput[] into a more useful arrangement
for (var i = 0; i < aInput.length; i++) {
rObject = {
start: parseInt(aInput[i][1]),
end: parseInt(aInput[i][2])
};
if (parseInt(aInput[i][0]) == 0){set++;}
input_indexed[set].push(rObject);
elem.push(parseInt(aInput[i][1]));
}
aOutput = [];
done = false;
while (!done) {
aOutput.push(elem.slice(0));
for (s = 0;s < input_indexed.length;s++){ //this is where I am trying to loop through the sets
for (i = elem.length - 1;; i--) {
if (i == -1) {
done = true;
break;
}
elem[i]++;
if (elem[i] <= input_indexed[s][i].end) {
break;
}
elem[i] = input_indexed[s][i].start;
}
}
}
console.log(aOutput);
答案 0 :(得分:1)
这是我提出的方法,以防它可以帮助任何人。
// JavaScript Document
var aInput = new Array();
aInput[0] = new Array("0", "1", "5");
aInput[1] = new Array("1", "1", "3");
aInput[2] = new Array("0", "10", "12");
aInput[3] = new Array("1", "10", "12");
var aOutput = [];
console.log(ProcessArray(aInput));
function ProcessArray(aInput){
var input_indexed = [], elem = []; // elem holds the row to be added to the output
// Get aInput[] into a more useful arrangement
for (var i = 0; i < aInput.length; i++) {
// if set is complete process it
if (i > 0 && parseInt(aInput[i][0]) == 0 && parseInt(aInput[i-1][0]) > 0){
aOutput = aOutput.concat(calcOutput(elem, input_indexed));
// clear set
elem = [];
input_indexed =[];
}
// if fist set is not there then create it
input_indexed[parseInt(aInput[i][0])] = {
start: parseInt(aInput[i][1]),
end: parseInt(aInput[i][2])
};
// Initialize elem with the start value of each column
elem.push(parseInt(aInput[i][1]));
}
// reset elem with the start value of each column
aOutput = aOutput.concat(calcOutput(elem, input_indexed));
return aOutput;
}
function calcOutput(elem, input_indexed){
// Produce the output
var aReturn = [];
done = false;
while (!done) {
aReturn.push(elem.slice(0)); // push a copy of elem into result
for (i = elem.length - 1;; i--) { // Increment elements from right to left
if (i == -1) { // We've run out of columns
done = true;
break;
}
elem[i]++; // Increment the current column
if (elem[i] <= input_indexed[i].end) {
// If it doesn't overflow, we're done
break;
}
// When it overflows, return to start value and loop around to next column
elem[i] = input_indexed[i].start;
}
}
return aReturn;
}