开发者

looping through arrays of arrays

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. H开发者_Go百科ow to print all those numbers? Its similar to html objects DOM


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


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


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


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


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


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.


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


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
    }
}


How to loop an indefinite nested arrays using Javascript - (Tendai Katsande)

function isArray(x) { if (typeof x === "object") { return true; } else { return false; } }

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


*How to loop an indefinite nested array in javascript *

function isArray(x) {
  if (typeof x === "object") {
    return true;
  } else {
    return false;
  }
}

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


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜