I need to populate a 3D array automatically, using natural positive numbers (0,1,2,3,4...), up to the array's full dimension. In this case, a 5x3x2 array stores 30 elements. Is there any algorithm, where for-loops could be employed to dynamically populate such array? For example: if I had a 5x3 2D array, I certainly could use the following code, to automatically generate its elements:
var ray = new Array(5);
for (var make2D = 0; make2D < ray.length; make2D++) {
ray[make2D] = new Array(3);
}
for (var i = 0; i < array.length; i++) {
for (var j = 0; j < array[i].length; j++) {
array[i][j] = i * array[i].length + j;
}
}
The above code would first create a 5 x 3 array and populate this 2D array with elements from 0 to 14. But, I have struggled to find a 'formula' to populate a 3D array likewise
Like explicit: array[ i ] [ j ] [ z ]= '.....code.....' using length property values and for-loops?