简体   繁体   English

Javascript中的高阶函数

[英]Higher-order functions in Javascripts

I am trying to use higher-order functions to fills a 3x3 2d array. 我正在尝试使用高阶函数来填充3x3 2d数组。 To be specific, i need to use array.map function to finish this. 具体来说,我需要使用array.map函数来完成此操作。 so far my code is: 到目前为止,我的代码是:

    function Matrix(m,n){
      var mat = Array.apply(null, new Array(m)).map(
       Array.prototype.valueOf,
       new Array(n)
     );
     return mat;
    }
    restaurants = Matrix(3,3);
    restaurants.map(
      function(row,i){
        return row.map(function(cell,j){
           return new BorderedCell(ToString(ancestry[i][j]));
           });
      });

There is no output when I try to display restaurants. 当我尝试显示餐厅时没有输出。 it seems like the code stoped at 似乎代码停在了

    function(row,i){}

and won't go further. 而且不会走得更远。

How can I fix this? 我怎样才能解决这个问题? Any help will be appreciated. 任何帮助将不胜感激。

Edit: I am trying to fill A 2D array with object BorderedCell. 编辑:我试图用对象BorderedCell填充一个2D数组。 My problem is I don't know how to use double mapping to go through the whole matrix, which I supposed to do. 我的问题是我不知道该如何使用双重映射来遍历整个矩阵。

If expected result of restaurants is [[0,1,2],[0,1,2],[0,1,2]] you can use Array.from() , Array.prototype.keys() 如果restaurants预期结果是[[0,1,2],[0,1,2],[0,1,2]] ,则可以使用Array.from()Array.prototype.keys()

 function arr(len) { return Array.from(Array(len).keys()) } function Matrix(m, n) { return arr(m).map(arr.bind(null, n)); } var restaurants = Matrix(3, 3); console.log(restaurants); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM