繁体   English   中英

如何使用ramda映射跟踪双数组项目索引?

[英]How do I keep track of double array items index with ramda map?

我有这个数组: [[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)

这行不通。

基于@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...

暂无
暂无

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

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