需要了解Javascript中的此代码段(转置数组)的帮助吗
我在另一篇文章中找到了转置数组的这段代码,但我不明白为什么我们只处理数组的第一个元素需要了解Javascript中的此代码段(转置数组)的帮助吗,javascript,arrays,algorithm,matrix,transpose,Javascript,Arrays,Algorithm,Matrix,Transpose,我在另一篇文章中找到了转置数组的这段代码,但我不明白为什么我们只处理数组的第一个元素a[0]。此外,下划线的作用是什么?(当我搜索下划线时,我得到的只是下划线库) 该代码确实转置了矩阵(2d数组) 外部map调用对a[0]进行操作,以获取列索引并生成“columns”长的输出数组(如果输入为amn,则返回一个包含n个条目的新数组)。没有使用第一个参数,因此使用了占位符标识符(\uu)。第二个参数(列索引)在内部映射中用于访问正确的单元格 在该调用中,进行了另一个map调用,这次是对整个数组进行调
a[0]
。此外,下划线的作用是什么?(当我搜索下划线时,我得到的只是下划线库)
该代码确实转置了矩阵(2d数组) 外部
map
调用对a[0]
进行操作,以获取列索引并生成“columns”长的输出数组(如果输入为amn,则返回一个包含n个条目的新数组)。没有使用第一个参数,因此使用了占位符标识符(\uu
)。第二个参数(列索引)在内部映射中用于访问正确的单元格
在该调用中,进行了另一个
map
调用,这次是对整个数组进行调用,它有效地映射了行。每一行(一个单元格数组)都转换为单值r[c]
,它们一起生成返回到外部映射的输出数组。代码确实转换了矩阵(2d数组)
外部map
调用对a[0]
进行操作,以获取列索引并生成“columns”长的输出数组(如果输入为amn,则返回一个包含n个条目的新数组)。没有使用第一个参数,因此使用了占位符标识符(\uu
)。第二个参数(列索引)在内部映射中用于访问正确的单元格
在该调用中,进行了另一个
map
调用,这次是对整个数组进行调用,它有效地映射了行。每一行(一个单元格数组)都被转换为单值r[c]
,它们一起生成返回到外部映射的输出数组。让我们展开代码以提高可读性,并浏览它:
// Pass an array to transpose: i.e. transpose(myArray);
function transpose(a){
// return the array returned from calling the .map method
// on the first item in the array (this assumes that the first
// item in the array is, itself, an array:
return a[0].map(
// Provide the function for the map to use
// This function can take 3 arguments for:
// currentValue, index, array
// The code below would access currentValue with _
// and index with c
function (_, c) {
// The mapping callback function's job is to return
// values that will be placed into a new array that
// the overall .map method creates. Here, the callback
// is calling .map on the original array:
return a.map(
// This callback is using the variable r to
// receive the currentValue of the array element
// being looped over (which is assumed to be an
// array itself here:
function(r) {
// This function returns elemeent at index
// position: c from the array element r and
// adds this to the new array being created
// by the nested .map call
return r[c];
});
});
}
现在,作者只需选择:u、c和r的参数名,但是对于.map()
,最好使用val
、index
和arr
(或非常类似的名称)来记住.map()
回调函数的参数所代表的内容
此函数用于传入数组(a
)的第一个元素,即a[0]
,它本身就是一个数组
然后,它遍历原始数组的元素,在与第一个数组相对应的索引位置抓取元素,但从第二个数组抓取元素。为了提高可读性,让我们展开代码并遍历它:
// Pass an array to transpose: i.e. transpose(myArray);
function transpose(a){
// return the array returned from calling the .map method
// on the first item in the array (this assumes that the first
// item in the array is, itself, an array:
return a[0].map(
// Provide the function for the map to use
// This function can take 3 arguments for:
// currentValue, index, array
// The code below would access currentValue with _
// and index with c
function (_, c) {
// The mapping callback function's job is to return
// values that will be placed into a new array that
// the overall .map method creates. Here, the callback
// is calling .map on the original array:
return a.map(
// This callback is using the variable r to
// receive the currentValue of the array element
// being looped over (which is assumed to be an
// array itself here:
function(r) {
// This function returns elemeent at index
// position: c from the array element r and
// adds this to the new array being created
// by the nested .map call
return r[c];
});
});
}
现在,作者只需选择:u、c和r的参数名,但是对于.map()
,最好使用val
、index
和arr
(或非常类似的名称)来记住.map()
回调函数的参数所代表的内容
此函数用于传入数组(a
)的第一个元素,即a[0]
,它本身就是一个数组
然后,它遍历原始数组的元素,在与第一个数组相对应的索引位置抓取元素,但从第二个数组抓取元素
下划线的作用是什么
map()
的第一个参数之所以命名为。
是因为它没有被使用。
下划线用作未使用参数的名称
但我不明白为什么我们只作用于数组a[0]的第一个元素
使用[0]是任意的。由于矩阵中的所有行的长度都相等,所以任何索引都可以工作。我们这样做是为了获得列数
基本上,第一个map()
迭代第一行(所有列),每次迭代时,忽略当前列值并获取当前列索引。换句话说,第一个map()
第二个map()
位于第一个map()的内部。这意味着它,对于每个列索引,迭代所有行(矩阵的高度),并在每次迭代中使用相应的列索引从当前行创建新行
- 需要注意的重要一点是,
map
每次都会创建一个新的数组,因此您要创建一个新的转置矩阵,而不是以任何方式更改初始矩阵李>
可视示例
如果从矩阵开始:
[ ['a1', 'a2', 'a3'] ]
[ ['b1', 'b2', 'b3'] ]
然后,在每一步中,都会出现以下情况(对原始矩阵使用M
,对转置矩阵使用T
):
最终得到一个转置矩阵:
// Step1
columnIndex = 0
T[columnIndex][0] becomes M[0][columnIndex] which is "a1"
T[columnIndex][1] becomes M[1][columnIndex] which is "b1"
transposed row 0 becomes ["a1", "b1"]
// Step2
columnIndex = 1
T[columnIndex][0] becomes M[0][columnIndex] which is "a2"
T[columnIndex][1] becomes M[1][columnIndex] which is "b2"
transposed row 1 becomes ["a2", "b2"]
// Step3
columnIndex = 2
T[columnIndex][0] becomes M[0][columnIndex] which is "a3"
T[columnIndex][1] becomes M[1][columnIndex] which is "b3"
transposed row 2 becomes ["a3", "b3"]
[ ['a1', 'b1'] ]
[ ['a2', 'b2'] ]
[ ['a3', 'b3'] ]
下面是更改了格式和变量名的代码,以更清楚地显示发生了什么。打开控制台查看每个步骤中发生的情况
var矩阵=[];
矩阵推送(['a1','a2','a3']);//第一排
矩阵推送(['b1','b2','b3']);
/*
矩阵=
[a1',a2',a3']
[['b1','b2','b3']]
*/
函数转置(矩阵){
var firstRow=矩阵[0];
var transposedMatrix=firstRow.map(函数(未使用,列索引){
调试('\n列索引=%d',列索引);
var transposedRow=matrix.map(函数(行,idx){//
下划线的作用是什么
map()
的第一个参数之所以命名为。
是因为它没有被使用。
下划线用作未使用参数的名称
但我不明白为什么我们只作用于数组a[0]的第一个元素
使用[0]
是任意的。因为矩阵中的所有行都具有相同的长度,所以任何索引都可以工作。我们这样做是为了获得列数
本质上,第一个map()
迭代第一行(所有列),每次迭代时,忽略当前列值并获取当前列索引。换句话说,第一个map()
从左到右迭代矩阵的“宽度”