Friday, May 10, 2024
 Popular · Latest · Hot · Upcoming
193
rated 0 times [  197] [ 4]  / answers: 1 / hits: 87981  / 13 Years ago, thu, august 18, 2011, 12:00:00

I have an arrays of arrays (some thing like graph), How to iterate all arrays?



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]]
];


Its just an example array, actual can contains any number of array and then arrays. How to print all those numbers? Its similar to html objects DOM


More From » arrays

 Answers
404

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);

[#90550] Wednesday, August 17, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
grant

Total Points: 169
Total Questions: 96
Total Answers: 98

Location: Cape Verde
Member since Sat, Apr 24, 2021
3 Years ago
;