Note that there are some explanatory texts on larger screens.

plurals
  1. POJavascript. Create multi dimensional array from calculation Part 2
    text
    copied!<p>I am trying to add more functionality to the function Barmar helped me create in <a href="https://stackoverflow.com/questions/16289780/javascript-create-multi-dimensional-array-from-calculation">part one</a>. Basicly I am creating a multi dimensional array that will count out ranges of numbers from a start and end number. Where it gets a little tricky for me is child ranges need to be nested inside the previous ranges. This is indicated with the first item in the aInput array position. For example The following input array would produce a new array with a length of 30 rows, and 3 columns per row.</p> <pre><code>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"); </code></pre> <p>The output would look something like this:</p> <pre><code>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 </code></pre> <p>As you can see I have expanded row 7 and it has 3 items (columns) inside of it. The full array counts from 1-5 with a nested rage inside counting the second line of the input array 1-3 and that one has another nested range inside of it counting 1-2. Simple concept, but a little hard to explain. Basically we are creating a number grid. Thanks to Barmar this all works fine. The new issue is I need to create multiple number grids and stack them on top of each other. Meaning my input array could look like this.</p> <pre><code>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"); </code></pre> <p>And the result should be an array that has a length of 42 and two columns. I have an idea of how that should work. I need to add another loop for the sets. Which I define in the input_indexed array, but I am having some issues understanding how the push works on a multi dimensional array. This is what I have so far. Any help is greatly appreciated. </p> <pre><code>// 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 &lt; 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 &lt; 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] &lt;= input_indexed[s][i].end) { break; } elem[i] = input_indexed[s][i].start; } } } console.log(aOutput); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload