如何使用ramda map跟踪双数组项索引?

问题描述 投票:0回答:1

我有这个数组:[[x,x,x,x][x,x,x,x][x,x,x,x][x,x,x,x]]

我想循环并转换这些项并返回一个新数组。所以我需要跟踪数组和数组项索引:

const mapIndexed = R.addIndex(R.map)
const columns = mapIndexed((col, cidx) => // I need ridx here)
const nextGrid = mapIndexed((row, ridx) => columns, currentGrid)

这不起作用。

javascript functional-programming ramda.js
1个回答
0
投票

基于@ bergi对mapIndexed的建议

const matrix = [
    ['a1', 'b1', 'c1', 'd1'],
    ['a2', 'b2', 'c2', 'd2'],
    ['a3', 'b3', 'c3', 'd3'],
    ['a4', 'b4', 'c4', 'd4'],
]

const mapIndexed = addIndex(map)

const map2d = (f, l) => 
  mapIndexed((row, x) =>
             mapIndexed((col, y) => f(col, x, y), row), l)

map2d((...x) => x, matrix)
// => [[["a1", 0, 0], ["b1", 0, 1], ["c1", 0, 2] etc...
© www.soinside.com 2019 - 2024. All rights reserved.