Array.map 是什么概念? [英] What is the concept of Array.map?
问题描述
我在理解 Array.map
的概念时遇到问题.我确实去了 Mozilla 和 Tutorials Point,但他们提供的相关信息非常有限.
I am having problems understanding the concept of Array.map
. I did go to Mozilla and Tutorials Point, but they provided very limited info regarding this.
这就是我使用 Array.map
的方式.有点复杂(有点 d3.js 的内容;忽略它)
This is how I am using Array.map
. It is a little complex (a bit of d3.js involved; just ignore it)
var mapCell = function (row) {
return columns.map(function(column) {
return { column : column, value : getColumnCell(row, column) }
})
}
//getColumnCell is a function defined in my code
//columns is array defined at the top of my code
我不明白这段代码在做什么.我知道它返回一个新的数组和东西,但这部分有点棘手!
I do not understand exactly what this code is doing. I know its returning a new array and stuff but this part is a little tricky!
如果你想通过我的代码:http://jsfiddle.net/ddfsb/2/一个>
if you want to go through my code: http://jsfiddle.net/ddfsb/2/
更新 1
我正在使用控制台来真正了解代码中发生的事情.看了大家提供的答案,我已经清楚地理解了array.map
的概念.现在剩下的唯一部分是参数行和列,但是提供的小提琴中的行和行以及列和列之间存在差异
I am using console to actually understand whats happening inside the code. Looking at the answers provided, I have clearly understood the concept of array.map
. Now the only part remaining is parameters rows and columns, but there is a difference between row and rows,and column and columns in the fiddle provided
var rows//completely ok
var columns//completely ok
funcion(row)//here,source of row is unknown.getColumncell function utilizes this parameter further making it more critical
function(column)//source of column is unknown..getColumncell function utilizes this parameter further making it more critical
有什么帮助吗??
推荐答案
我们稍微改写一下,从内到外开始工作.
Let's rewrite it a bit, and start working from inside out.
var mapCell = function (row) {
return columns.map(
function(column) {
return {
column : column,
value : getColumnCell(row, column)
}
}
)
}
function(column)
部分本质上是一个以列为参数的函数,并返回一个具有两个属性的新对象:
The function(column)
part is essentially a function that takes a column as a parameter, and returns a new object with two properties:
- 列,即参数的原始值,以及
- value,即对行(外部变量)和列(参数)调用getColumnCell函数的结果
columns.map()
部分调用 Array.map
函数,它接受一个数组和一个函数,并对它的最后一项运行该函数,并返回结果.即如果输入是数组 [1, 2, 3, 4, 5]
并且函数类似于 isEven
,结果将是数组 [假,真,假,真,假]
.在您的情况下,输入是列,输出是对象列表,每个对象都有一个列和一个值属性.
The columns.map()
part calls the Array.map
function, that takes an array and a function, and runs the function for every last item of it, and returns the results. i.e. if the input is the array [1, 2, 3, 4, 5]
and the function is something like isEven
, the result will be the array [false, true, false, true, false]
. In your case, the input are the columns, and the output is a list of objects, each of which has a column and a value properties.
最后,var mapCell = function (row)
部分声明变量 mapCell
将包含一个名为 row
的变量的函数 -这与在内部函数中使用的 row
相同.
Lastly, the var mapCell = function (row)
part declares that the variable mapCell
will contain a function of one variable called row
- and this is the same row
that is used in the inner function.
在一个句子中,这行代码声明了一个函数,该函数在运行时将取一行并返回该行所有列的值.
In a single sentence, this line of code, declares a function that when run, will take a row and return values for all columns for that row.
这篇关于Array.map 是什么概念?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!