Array.map的概念是什么? [英] What is the concept of Array.map?

查看:210
本文介绍了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:

  • 列,即参数的原始值,
  • 值,这是在行(外部变量)和列(参数)上调用getColumnCell函数的结果

columns.map()部分调用 Array.map 函数,它接受一个数组和一个函数,并为它的每个最后一项运行该函数,并返回结果.即如果输入是数组[1, 2, 3, 4, 5]并且函数是类似isEven的东西,则结果将是数组[false, true, false, true, false].在您的情况下,输入是列,输出是对象列表,每个对象都有一个列和一个值属性.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆