简体   繁体   English

二维数组上的 Javascript 映射

[英]Javascript map over two dimensional array

I have this array:我有这个数组:

rows = [ [ 89, 18, 9 ], [ 1903, 3, 4 ], [ 3, 1, 800 ] ];

It should look like this:它应该是这样的:

[ [ 89, 1903, 3 ], [ 18, 3, 1 ], [ 9, 4, 800 ] ]

And the code, that is working, looks like this:有效的代码如下所示:

rows[0].map((_, columnIndex) => rows.map(
            row => row[columnIndex])
        );

How does this work?这是如何运作的?

I'll assume you are simply not accustomed to the particular language features being used here, hence why you can't follow what is going on, so here goes:我假设您只是不习惯这里使用的特定语言功能,因此您无法理解正在发生的事情,所以这里是:

  • Your structure is a nested Array .您的结构是一个嵌套的Array Hence the nested Array.map s.因此嵌套的Array.map s。

  • Both map callbacks make use of implicit return .两个map回调都使用隐式 return

which unfolds to this:展开到这个:

rows[0].map((row, index) => {
  return rows.map((column) => {
    return column[index]
  })
})

The 2 arguments passed to the map callback are the following:传递给map回调的 2 个参数如下:

  • element : The currently iterated Array element; element : 当前迭代的 Array 元素; In your first map this is the row argument.在您的第一张map中,这是row参数。
  • i : The current iteration number, starting from 0; i : 当前迭代次数,从0开始; In your first map this is the index argument.在您的第一张map中,这是index参数。

That's all there is to it.这里的所有都是它的。 From then on you just follow the iterations and the values of each argument at each iteration.从那时起,您只需关注迭代和每次迭代中每个参数的值。

                  +--- The outter function map gets the first array to loop through the rows
[ 89,   18, 9   ] |
[ 1903, 3,  4   ] |
[ 3,    1,  800 ] v
  +--->
  |
  +- The nested function map is looping through the columns.
     The key here is the fixed column using index (column[index])
     from the outter function map, so every iteration from the outter
     function map will fix the access to that column, i.e -
     index = 0 will access the array as follow: array[j][0], array[j+1, 0], ... array[n, 0]
                                                         ^              ^                ^

This is an approach to illustrate what's happening using direct index accesses.这是一种使用直接索引访问来说明正在发生的事情的方法。

 var rows = [ [ 89, 18, 9 ], [ 1903, 3, 4 ], [ 3, 1, 800 ] ]; var result = []; for (var i = 0; i < rows[0].length; i++) { result[i] = new Array(rows[0].length).fill(); for (var j = 0; j < rows.length; j++) { result[i][j] = rows[j][i]; // Here is the fixed column access using the outter index i. } } console.log(result);
 .as-console-wrapper { max-height: 100%;important: top; 0; }

Iterator Methods (like map , forEach , filter ...) dealing with array 2d as every element is 1d array迭代器方法(如mapforEachfilter ...)处理二维数组,因为每个元素都是一维数组

For example:例如:

    arr= [[1,0,0],
          [0,1,0],
          [0,0,1]]
    arr.map( (item)=> {
           item.forEach( (element)=> {
             //...
            //...
           })
    })

The first iterator ( map ) take the first row in arr array in this example [1,0,0]在此示例中,第一个迭代器 ( map ) 获取 arr 数组中的第一行[1,0,0]

The second iterator takes the second row of arr that is [0,1,0] save it in item and so on...第二个迭代器将 arr 的第二行[0,1,0]保存在 item 中,依此类推...

In a nested loop ( foreach ) that take the real number like 0 or 1 .在采用01等实数的嵌套循环 ( foreach ) 中。 Here the code can deal with it.这里的代码可以处理它。

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

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