如何使用 Ramda 复制对象数组并仅更改对象索引?

How to copy an array of object and change only objects indexes using Ramda?

我收到这样的数组: 步骤:[{ One: 1, Two: 2, }, {One: 3, Two: 4, }]

我想复制这个数组 Steps 只是将索引更改为小写,如 onetwo,复制值..

我如何使用 ramda 做到这一点?

此通用 mapKeys 函数接受另一个函数来转换键(在本例中为 R.toLower)。该函数将对象转换为 R.toPairs 的条目,使用提供的函数 (cb) 演化每个条目的第一项(键),然后转换使用 R.fromPairs.

返回对象的条目

const { pipe, toPairs, map, evolve, fromPairs, toLower } = R

const mapKeys = cb => pipe(
  toPairs,
  map(evolve([cb])),
  fromPairs,
)

const steps =  [{ One: 1, Two: 2, }, { One: 3, Two: 4 }]

const result = map(mapKeys(toLower))(steps) // call mapKeys on an array of objects

console.log(result)
<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.27.0/ramda.js"></script>