looping through arrays of arrays
For 2 dimenional Arrays:
for(var i = 0; i < parentArray.length; i++){
for(var j = 0; j < parentArray[i].length; j++){
console.log(parentArray[i][j]);
}
}
For arrays with an unknown number of dimensions you have to use recursion:
function printArray(arr){
for(var i = 0; i < arr.length; i++){
if(arr[i] instanceof Array){
printArray(arr[i]);
}else{
console.log(arr[i]);
}
}
}
if you just want to print all the members,how about this?
var items = parentArray.toString().split(",");
for(var i=0,j=items.length;i<j;i++)
console.log(items[i]);
what you need to do is something like this
var parentArray = [
[[1,2,3],[4,5,6],[7,8,9]],
[[10,11,12],[13,14,15],[16,17,18]],
[[19,20,21],[22,23,24],[26,27,28]]
];
for(int i = 0; i < parentArray.length;i++){
var value = parent[i];
for(int j = 0; j < parent[i].length; j++){
var innerValue = parent[i][j];
}
}
So this will be like a nested loop, then there where innerValue and value is you can do some operations, hope it helps
This recursive function should do the trick with any number of dimensions:
var printArray = function(arr) {
if ( typeof(arr) == "object") {
for (var i = 0; i < arr.length; i++) {
printArray(arr[i]);
}
}
else document.write(arr);
}
printArray(parentArray);