2013-05-02 53 views
0

我想更多的功能添加到功能Barmar幫我part one創建。基本上,我正在創建一個多維數組,它將從開始和結束數字中計算出數字的範圍。對於我來說有點棘手的是,孩子的範圍需要嵌套在先前的範圍內。這通過aInput數組中的第一項指示。例如,以下輸入數組將生成一個長度爲30行,每行3列的新數組。Javascript。創建一個從計算部分多維數組2

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); 
+0

我想通了。我會很快發佈答案 – user2300933 2013-05-02 21:53:21

回答

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; 

}