Array.map的概念是什么?

时间:2013-06-28 15:06:47

标签: javascript jquery arrays mozilla array-map

我在理解Array.map的概念时遇到了问题。我确实去过Mozilla和Tutorials Point,但他们提供的信息非常有限。

这就是我使用Array.map的方式。它有点复杂(涉及一点d3.js;只是忽略它)

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

我不明白这段代码到底在做什么。我知道它返回了一个新阵列和东西,但这部分有点棘手!

如果您想查看我的代码:http://jsfiddle.net/ddfsb/2/

更新1

我正在使用控制台来实际了解代码中发生的事情。看看提供的答案,我清楚地理解了array.map的概念。现在剩下的唯一部分是参数行和列,但行和行之间存在差异,并且提供的小提琴中的列和列

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

任何帮助??

9 个答案:

答案 0 :(得分:17)

让我们重写一下,然后从内到外开始工作。

var mapCell = function (row) {
  return columns.map(
    function(column) {
      return { 
        column : column, 
        value : getColumnCell(row, column)
      }
    }
  )
}

function(column)部分本质上是一个以列为参数的函数,并返回一个具有两个属性的新对象:

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

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

最后,var mapCell = function (row)部分声明变量mapCell将包含一个名为row的变量的函数 - 这与row中使用的相同{{1}}内在的功能。

在一行中,这行代码声明了一个函数,该函数在运行时会占用一行并返回该行所有列的值。

答案 1 :(得分:3)

理解map函数只是解决方案的一部分,还有函数mapCell。它需要一个参数row并返回类似于:

的内容
[ {
    "column": "parties",
    "value": [cell value]
}, {
    "column": "star-speak",
    "value": [cell value]
} ]

单元格值取决于row和列(派对,星级等等)

map函数将变换应用于值,并返回该变换后的值。

一个简单的例子:

function square(x) { return x * x; }

[ 2, 3, 4 ].map(square); // gives: [ 4, 9, 16 ]

类似地:

[ "parties", "starspeak" ].map(function (column) {
    return {
        column: column,
        value: findTheValue(column)
    }
});

现在,因为该地图嵌套了一个获得row参数的函数。您可以在map函数中使用它来获取:

function (row) {
    return [ "parties", "starspeak" ].map(function (column) {
        return {
            column: column,
            value: findTheValue(row, column)
        }
    });
}

这非常接近你的代码。

答案 2 :(得分:2)

map循环遍历原始数组,并为数组中的每个值调用方法。它收集函数的结果以创建包含结果的新数组。您正在将值数组“映射”为新的映射值数组。您的代码相当于:

var mapCell = function (row) {
    var result = [];
        for (var i = 0; i < columns.length; ++i) {
            var mappedValue = {
                column: columns[i], 
                value : getColumnCell(row, columns[i])
            };
            result.push(mappedValue);
        }
    return result;
};

答案 3 :(得分:2)

Map function goes through each element of an array in ascending order and invokes function f on all of them. 
It returns new array which is being computed after function is invoked on it.

Ref: http://www.thesstech.com/javascript/array_map_method

Syntax
array.map(f)

Example:

<!doctype html>
<html>
 <head>
 <script>
   var arr = [4,5,6];
   document.write(arr.map(function(x){return x*2;}));
 </script>
 </head>
</html>

Answer: 8,10,12
Ref: http://www.thesstech.com/tryme?filename=javascript_array_map_method

答案 4 :(得分:0)

可能大多数人来这里(像我一样)只想要一个基本的array.map用法示例:

myArray = [1,2,3]
mappedArray = [];

mappedArray = myArray.map(function(currentValue){
     return currentValue *= 2;
})

//myArray is still [1,2,3]
//mappedArray is now [2,4,6]

这是最基本的。有关其他参数,请查看:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map

答案 5 :(得分:0)

摘要

Array.map是位于Array.prototype.map上的函数。该函数执行以下操作:

  1. 使用相同数量的条目/元素创建新数组
  2. 执行一个回调函数,该函数接收当前数组元素作为参数,并返回新数组的条目。
  3. 返回新创建的数组。

示例:

基本用法:

const array = [1, 2, 3, 4];

// receive each element of array then multiply it times two
// map returns a new array
const map = array.map(x => x * 2);

console.log(map);

回调函数还公开了索引和原始数组:

const array = [1, 2, 3, 4];

// the callback function can also receive the index and the 
// original array on which map was called upon
const map = array.map((x, index, array) => {
  console.log(index);
  console.log(array);
  return x + index;
});

console.log(map);

答案 6 :(得分:0)

如果您有一个元素数组,则必须在该元素上执行相同的操作 您可以在该时间使用javascript map函数对数组中的每个元素进行数组化 它有助于迭代抛出数组,然后我们可以执行每个元素的操作, 退回它。

let NumberArray = [1,2,3,4,5,6,7,8];

let UpdatedArray = NumberArray.map( (Num , index )=>{ 
                return Num*10;
            })

console.log(UpdatedArray);

//UpdatedArray ==> [10, 20, 30, 40, 50, 60, 70, 80]

答案 7 :(得分:0)

Javascript map()语法

arrayObj.map(callback [,context]);

arrayObj是在其上调用map()的原始数组。

map()有2个命名参数,第一个是回调函数,第二个是上下文对象。回调函数在数组的每个元素上触发。

此外,回调函数带有3个参数:

函数回调(currentElement,索引,数组){

}

currentElement –这是传递给回调函数的数组的当前元素

index –当前元素的索引

array –应用了map()的完整数组

在这3个元素中,currentElement参数是必需的,而其余2个参数是可选的。

但是,map()不会更改原始数组,它会创建一个由回调函数生成的新数组元素。

您可以在JavaScript map function

上阅读更多内容

答案 8 :(得分:0)

Array map() 方法返回一个新数组。 它不会改变原始数组。

let array = arr.map((c, i, arr) => { //return element to new array });

这里,

  • array 是返回的新数组。
  • arr 是调用 map 方法的原始数组。
  • c 是正在处理的当前值。
  • i 是当前值的索引。

例如:-

const originalArr = [4, 3, 2]; let newArr = originalArr.map((val) => val + val);

结果:-

newArr: [8, 6, 4] 原文Arr: [4, 3, 2]