Reputation: 42454
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
Upvotes: 22
Views: 98717
Reputation: 1078
const 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]
]
]
parentArray.map(
childArray => childArray.map(
grandChildArray => grandChildArray.map(
arrayItem => console.log(arrayItem)
)
)
)
Upvotes: 0
Reputation: 41
How to loop an indefinite nested array in JavaScript:
function isArray(x) {
return typeof x === "object";
}
let x = [1, 2, 3, 4, 5, [5, [2, 3, 4], 3], [[[0]]]];
function loop_map(x, idx) {
x.map((res, i) => {
console.log("Index ", i);
if (isArray(res)) {
loop_map(res, i);
} else {
console.log(res);
}
});
}
loop_map(x, 0);
Upvotes: 0
Reputation: 11
I don't know if this is new or what, but it may please you to know that there is an Array prototype ".flat" that can be used here to make the array one-dimensional:
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]]
];
function flatten(){
flatArray = parentArray.flat(Infinity);
flatArray.forEach(function(item, i){
console.log(item)
})
}
Upvotes: 1
Reputation: 11453
Using array.forEach method
parentArray.forEach( function(childArray) {
childArray.forEach(function(item){
console.log(item);
});
});
[ 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 ]
One liner using => "fat arrow" in ES6+
parentArray.forEach(subarray => { subarray.forEach( item => {console.log(item); }); });
[ 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 ]
If you want to list individual number in item, add another layer of forEach.
Upvotes: 6
Reputation: 1900
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
Upvotes: 11
Reputation: 18041
One option would be to use recursion which works with any number of dephts. See traverse()
, it's not tested but should give a rough idea:
String.prototype.lpad = function(padString, length) {
var str = this;
while (str.length < length)
str = padString + str;
return str;
}
var level = 0;
function traverse(obj) {
if (obj instanceof Array) { level++; for(var i in obj) traverse(obj[i]); level--; }
else console.log(''.lpad('-', level) + obj);
}
Upvotes: 5
Reputation: 59
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]);
Upvotes: 5
Reputation: 90776
If you have a DOM like structure, you need to recursively iterate through the elements. Something like that should work:
function printArray(array) {
for (var i = 0; i < array.length; i++) {
var v = array[i];
if (v instanceof Array) {
printArray(v);
} else {
console.log(v);
}
}
}
Upvotes: 2
Reputation: 16091
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);
Upvotes: 23
Reputation: 7489
You would use nested for loops here. The outer loop would iterate the parent array, giving you one of the internal arrays each time. The inner loop would give you the items within each array. Example:
for(childArray in parentArray){
for(item in childArray){
//do stuff here to each number
}
}
Upvotes: 0
Reputation: 24534
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]);
}
}
}
Upvotes: 18