Array.map概念? [英] Array.map concept?

查看:248
本文介绍了Array.map概念?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有理解Array.map的概念问题。我没有去Mozilla和教程点,但他们提供了关于这个非常有限的信息。

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 but ignore)

 var mapCell = function (row){return columns.map(function(column) {return { column : column, value : getColumnCell(row, column) }})}
//getColumnCell is a function defined in my code
//clolumns is array defined at the top of my code

我不明白究竟这是什么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!

如果你想通过我的code - > http://jsfiddle.net/ddfsb/2/

if you want to go through my code-->http://jsfiddle.net/ddfsb/2/

更新1 - >

.I正在使用控制台真正了解什么在提供的答案的code.Lookking内部发生,我已清楚地理解array.map.Now的概念剩余的唯一部分是参数的行和列,但也有是小提琴提供行和行和列与列之间的差异

.I am using console to actually understand whats happening inside the code.Lookking 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)
      }
    }
  )
}

功能(栏)部分本质上是一个函数,它采用列作为参数,并返回一个新的对象具有两个属性:

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()部分调用<一个href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map\"><$c$c>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 =功能(行)部分声明变量 mapCell 将包含的功能一个变量名为 - 这是在同一个是在内部函数中使用

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.

在一个简单的句子,这条线code的,声明的函数中运行时,将采取行和该行的所有列返回值。

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天全站免登陆