I am trying to use higher-order functions to fills a 3x3 2d array. To be specific, i need to use array.map function to finish this. 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. 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()
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);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.