0

我正在尝试为 Barmar 在第一部分中帮助我创建的功能添加更多功能。基本上我正在创建一个多维数组,它将从开始和结束数字中计算出数字范围。对我来说有点棘手的地方是子范围需要嵌套在以前的范围内。这由 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 开始计数,其中嵌套的范围内计数输入数组的第二行 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);
4

1 回答 1

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;

}
于 2013-05-04T20:31:04.270 回答