Javascript. Create multi dimensional array from calculation Part 2 -
i trying add more functionality function barmar helped me create in part one. basicly creating multi dimensional array count out ranges of numbers start , end number. gets little tricky me child ranges need nested inside previous ranges. indicated first item in ainput array position. example following input array produce new array length of 30 rows, , 3 columns per row.
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");
the output this:
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
as can see have expanded row 7 , has 3 items (columns) inside of it. full array counts 1-5 nested rage inside counting second line of input array 1-3 , 1 has nested range inside of counting 1-2. simple concept, little hard explain. creating number grid. barmar works fine. new issue need create multiple number grids , stack them on top of each other. meaning input array this.
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");
and result should array has length of 42 , 2 columns. have idea of how should work. need add loop sets. define in input_indexed array, having issues understanding how push works on multi dimensional array. have far. appreciated.
// 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; // ainput[] more useful arrangement (var = 0; < 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)); (s = 0;s < input_indexed.length;s++){ //this trying loop through sets (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);
here method cam in case anyone.
// 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 row added output // ainput[] more useful arrangement (var = 0; < ainput.length; i++) { // if set complete process 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 not there create input_indexed[parseint(ainput[i][0])] = { start: parseint(ainput[i][1]), end: parseint(ainput[i][2]) }; // initialize elem start value of each column elem.push(parseint(ainput[i][1])); } // reset elem start value of each column aoutput = aoutput.concat(calcoutput(elem, input_indexed)); return aoutput; } function calcoutput(elem, input_indexed){ // produce output var areturn = []; done = false; while (!done) { areturn.push(elem.slice(0)); // push copy of elem result (i = elem.length - 1;; i--) { // increment elements right left if (i == -1) { // we've run out of columns done = true; break; } elem[i]++; // increment current column if (elem[i] <= input_indexed[i].end) { // if doesn't overflow, we're done break; } // when overflows, return start value , loop around next column elem[i] = input_indexed[i].start; } } return areturn; }
Comments
Post a Comment