【问题标题】:What is the concept of Array.map?Array.map 的概念是什么?
【发布时间】:2013-06-28 15:06:46
【问题描述】:

我在理解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

有什么帮助吗??

【问题讨论】:

    标签: javascript jquery arrays mozilla array-map


    【解决方案1】:

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

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

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

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

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

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

    在一个句子中,这行代码声明了一个函数,该函数在运行时将获取一行并返回该行所有列的值。

    【讨论】:

    • 我能知道这个列参数和行参数是从哪里来的吗?问题是我只是在不理解的情况下从网站上转储了这段代码,现在我很痛苦..
    • 这段代码中的假设是row参数被传递给函数,columns是一个叫做column的东西的集合,并且存在一个叫做getColumnCell的函数以一行和一列作为参数。仅从这个 sn-p 来看,不可能说出它们是什么或这些东西来自哪里。
    【解决方案2】:

    这里理解map函数只是部分解决方案,还有mapCell这个函数。它接受一个参数row 并返回如下内容:

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

    单元格值取决于row 和列(派对、明星发言等)

    映射函数将转换应用于一个值,并返回该转换后的值。

    一个简单的例子:

    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)
            }
        });
    }
    

    这与您的代码非常接近。

    【讨论】:

    • 我理解了 function(row) 和 .map(function (column){}....函数内部的这些参数导致了我的问题。这些参数来自哪里
    • 这些参数行和列是从哪里来的??
    • column 是columns 的元素,所以"parties"、"starspeak" 等row 是作为参数从其他地方传入的。
    【解决方案3】:

    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;
    };
    

    【讨论】:

      【解决方案4】:
      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
      

      【讨论】:

        【解决方案5】:

        总结

        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】:

          可能大多数来这里的人(比如我)只是想要一个基本的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

          【讨论】:

            【解决方案7】:

            如果您有一个元素数组,并且您必须对元素执行相同的操作 那个时候数组的每个元素都可以使用 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]
            

            【讨论】:

              【解决方案8】:

              Javascript map() 语法

              arrayObj.map(callback[,context]);

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

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

              另外,回调函数有 3 个参数:

              函数回调(currentElement,index,array){

              }

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

              index – 当前元素的索引

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

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

              然而,map() 并没有改变原来的数组,它创建了一个新的数组元素,由回调函数生成。

              您可以在JavaScript map function阅读更多内容

              【讨论】:

                【解决方案9】:

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

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

                这里,

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

                例如:-

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

                结果:-

                newArr: [8, 6, 4] originalArr: [4, 3, 2]

                【讨论】:

                  猜你喜欢
                  • 1970-01-01
                  • 2010-11-24
                  • 2012-04-17
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 2012-01-02
                  • 2012-10-04
                  • 2011-04-25
                  相关资源
                  最近更新 更多