0

How i can make a 3 multidemnsional array? And the called ir from "for"

i ask a question to a usser with prompt and arrays, and then i want to show the full info using a doc.write calling all the arrays (array with 2 sub arrays and 2 sub sub arrays inside one of the second dimension, is very messi but i hope it understand. thanks

10th May 2017, 11:27 PM
Julian Bona
Julian Bona - avatar
1 Respuesta
+ 3
Better to show us your actual code, to good understanding your data structure... and do you want do output only the raw data, or does it fit into a specific html structure ( template )? Anyway, iterating over a 3 dimentionnal array with 'for' statement is done like that: var arr = [ ]; var d1 = 5; // 1st dimension var d2 = 5; // 2nd dimension var d3 = 5; // 3rd dimension var i, j, k; // populating the array: for (i=0; i<d1; i++) { arr.push([ ]); for (j=0; j<d1; j++) { arr[i].push([ ]); for (k=0; k<d1; k++) { arr[i][j].push(Math.floor(100*Math.random())); } } } // output the array items in a simple list format var out = ''; for (i=0; i<d1; i++) { for (j=0; j<d1; j++) { for (k=0; k<d1; k++) { out += '['+i+','+j+','+k+'] : '+arr[i][j][k]+'\n'; } } } document.write('<pre>'+out+'</pre>');
11th May 2017, 12:15 AM
visph
visph - avatar