For loop in multidimensional javascript array

前端 未结 8 1530
我在风中等你
我在风中等你 2020-11-28 21:28

Since now, I\'m using this loop to iterate over the elements of an array, which works fine even if I put objects with various properties inside of it.

var cu         


        
8条回答
  •  余生分开走
    2020-11-28 22:02

    You can do something like this:

    var cubes = [
     [1, 2, 3],
     [4, 5, 6],    
     [7, 8, 9],
    ];
    
    for(var i = 0; i < cubes.length; i++) {
        var cube = cubes[i];
        for(var j = 0; j < cube.length; j++) {
            display("cube[" + i + "][" + j + "] = " + cube[j]);
        }
    }
    

    Working jsFiddle:

    • http://jsfiddle.net/TRR4n/

    The output of the above:

    cube[0][0] = 1
    cube[0][1] = 2
    cube[0][2] = 3
    cube[1][0] = 4
    cube[1][1] = 5
    cube[1][2] = 6
    cube[2][0] = 7
    cube[2][1] = 8
    cube[2][2] = 9
    

提交回复
热议问题