简体   繁体   English

水平遍历2D数组-javascript

[英]loop through 2D array horizontally - javascript

how do loop through this 3x3 grid of arrays horizontally and print out 1, 4, 7, 2, 5, 8, 3, 6, 9? 如何水平遍历这个3x3的阵列网格并打印出1,4,7,2,5,5,8,3,6,9?

edit: is there a more efficient way of doing this other than to use a two loops that also works if the length of the arrays are not equal to the other ones? 编辑:除了使用两个循环(如果数组的长度不等于其他长度)时,还可以使用两个循环吗?

 let grid = [ [1, 2, 3], [4, 5, 6], [7, 8, 9] ]; 

You can use nested for Loops: 您可以for循环使用嵌套:

 let grid = [ [1, 2, 3], [4, 5, 6], [7, 8, 9], ]; for(let i = 0; i < grid[0].length; i++){ for(let j = 0; j < grid.length; j++) console.log(grid[j][i]); } 

for(var i = 0; i < grid.length; i++) {
    var arr = grid[i];
    for(var j = 0; j < arr.length; j++) {
        console.log("array[" + i + "][" + j + "] = " + arr[j]);
    }
}

 let grid = [ [1, 2, 3], [4, 5, 6], [7, 8, 9] ]; let gridLength = grid[0].length; let gridHeight = grid.length; for(let l1=0; l1<gridLength;l1++) { for(let l2=0; l2<gridHeight;l2++) { console.log(grid[l2][l1]); } } 

Assuming grid is rectangular(and grids usually are ;)), you can loop through it, columns first, rows second like in this snippet. 假设网格是矩形的(网格通常是;)),则可以像下面的代码片段那样循环遍历它,首先是列,然后是行。

There was a question if we can do it without 2 loops, we can and I think its actually better and cleaner solution: 有一个问题,我们是否可以不使用两个循环就可以做到,我可以,而且我认为它实际上是更好,更干净的解决方案:

 let grid = [ [1, 2, 3], [4, 5, 6], [7, 8, 9] ]; let gridLength = 3; for(let l1=0; l1<gridLength*gridLength;l1++) { console.log(grid[l1%gridLength][Math.floor(l1/gridLength)]); } 

You have a lot of answers that work if the arrays are all the same length. 如果数组的长度都相同,那么会有很多答案可以解决。 Here's a way to handle potentially different length arrays. 这是处理可能不同长度的数组的一种方法。 It basically runs until it can't find any more data: 它基本上运行直到找不到更多数据为止:

 let grid = [ [1, 2, 3, 4, 5], [4, 5, 6, 7], [7, 8, 9, 10, 11, 12] ]; let current = grid, i = 0 while(current.length){ current = grid.filter(arr => i < arr.length) current.forEach(arr => console.log(arr[i])) i++ } 

You need to make use of a nested for loop : 您需要使用嵌套的for循环

So please have a look at my solution: 因此,请看一下我的解决方案:

 let grid = [ [1,2,3], [4,5,6], [7,8,9] ]; for (let x = 0; x < grid[0].length; x++) { for (let y = 0; y < grid.length; y++) { console.log(grid[y][x]); } } 


This grid prints out the expected solution: 1, 4, 7, 2, 5, 8, 3, 6, 9? 此网格打印出预期的解决方案: 1, 4, 7, 2, 5, 8, 3, 6, 9?


Some explanation : 一些解释

In this case we are using two different for-loops . 在这种情况下,我们使用两个不同的for-loops

  • the outer one for the X for (let x = 0; x < 3; x++) {} X的外部表示for (let x = 0; x < 3; x++) {}
  • the inner one for the Y for (let y = 0; y < 3; x++) {} Y的内部for (let y = 0; y < 3; x++) {}

Note : This will just work if the grid is rectangular because using grid[0].length will otherwise produce an error. 注意 :这仅在网格为矩形的情况下有效,因为使用grid[0].length会产生错误。


Edit : If you just want to use a single loop try something like this: 编辑 :如果您只想使用一个循环,请尝试如下操作:

 let grid = [ [1, 2, 3], [4, 5, 6], [7, 8, 9] ]; for (let i = 0 ; i < grid.length * grid[0].length ; i++) console.log(grid[i % grid.length][parseInt(i / grid.length)]); 

you just have to find the sizes of the inner array and outer array 您只需要找到内部数组和外部数组的大小

for(let i=0;i<countYourArraySize;i++)
{
for(let j=0;j<countYourInnerArayLength;j++)
{
console.log(grid[j][i])
}
}

You can do it with just one (explicit) loop if you use map : 如果使用map ,则只需一个(显式)循环即可:

let grid = [
 [1,2,3],
 [4,5,6],
 [7,8,9]
];
let result = [];
for (let i=0; i < grid.length; i++) {
    result = result.concat(grid.map((row) => row[i])));
}
console.log(result);

You can do this in a single loop. 您可以在一个循环中完成此操作。 But the length must same for this 但长度必须与此相同

let grid = [
  [1, 2, 3],
  [4, 5, 6],
  [7, 8, 9]
];

for (var i = 0; i < grid.length*grid.length; i++) {
    var row = Math.floor(i / grid.length);
    var col = i % grid.length;
    console.log(grid[col][row]);
}
let grid = [
     [1,2,3],
     [4,5,6],
     [7,8,9]
];
for(let x = 0; x < 3; x++){
    for(let y = 0; y < 3; y++){
       console.log(grid[y][x]);
    }
}

Code here is data specific 这里的代码是特定于数据的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM