高级 Ramda 数组转换

Advanced Ramda Array Transformation

TLTR: 查看 javascript 片段。

我有一列物品。

我想映射项目并更改它们的结构。

我需要保留一些属性,我还需要设置一个新的 属性。但是,新的 属性 值将基于我当前映射的项目。

// change title to whatever shape you desire
const customTitleTransformation = title => title

const arrayOfItems = [
  {
    id: 'some id 1',
    isSelected: true,
    title: 'some title 1',
    text: 'some text',
    description: 'some description'
  },
  {
    id: 'some id 2',
    isSelected: false,
    title: 'some title 2',
    text: 'some text',
    description: 'some description'
  },
]

// I need array of items like:
// {
//   id,
//   isSelected,
//   cells: [
//     customTitleTransformation(title),
//     text
//   ]
// }

// REGULAR JAVASCRIPT WAY  
const normalizedItems = arrayOfItems.map(item => ({
  id: item.id,
  isSelected: item.isSelected,
  cells: [
    customTitleTransformation(item.title),
    item.text,
  ]
}))

console.log('first result ', normalizedItems)

// USING RAMDA  

const normalizedItemsRamda = R.map(
  R.compose(
    R.pick(['id', 'isSelected', 'cells']),
    R.set(R.lensProp('cells'), ['how do I set the array?'])
  )
)(arrayOfItems)

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

无论怎么想,我都不是 Ramda 或 FP 方面的专家,但似乎最简单的方法是将一个普通函数传递给 R.compose 以添加单元格 [=24] =] 在 pick 设置您想要的属性之前添加到每个对象上。

让我知道这是否适合您,或者如果这是一种反模式并且您正在严格寻找 Ramda 内置函数。

// change title to whatever shape you desire
const customTitleTransformation = title => title

const arrayOfItems = [
  {
    id: 'some id 1',
    isSelected: true,
    title: 'some title 1',
    text: 'some text',
    description: 'some description'
  },
  {
    id: 'some id 2',
    isSelected: false,
    title: 'some title 2',
    text: 'some text',
    description: 'some description'
  },
]

// USING RAMDA  

const normalizedItemsRamda = R.map(
  R.compose(
    R.pick(['id', 'isSelected', 'cells']),
    // shallow copy, add cells property, return new updated object
    item => R.assoc('cells', [customTitleTransformation(item.title), item.text], item)
  )
)(arrayOfItems)

console.log('ramda result ', normalizedItemsRamda)
console.log('\n\nOriginal Items Unchanged:')
console.log(arrayOfItems);
<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.25.0/ramda.js"></script>

我认为这可能是 applySpec 的情况。

mapObject函数定义新对象的形状。然后将该转换应用于 arrayOfItems.

当然,这是否更易读完全取决于您的评价:

// change title to whatever shape you desire
const customTitleTransformation = title => title

const arrayOfItems = [{
    id: 'some id 1',
    isSelected: true,
    title: 'some title 1',
    text: 'some text',
    description: 'some description'
  },
  {
    id: 'some id 2',
    isSelected: false,
    title: 'some title 2',
    text: 'some text',
    description: 'some description'
  },
]

const mapObject = R.applySpec({
  id: R.prop('id'),
  isSelected: R.prop('isSelected'),
  cells: R.juxt([
    R.o(customTitleTransformation, R.prop('title')),
    R.prop('text')
  ])
});

const normalizedItemsRamda = R.map(mapObject, arrayOfItems)

console.log('ramda result ', normalizedItemsRamda)
console.log('\n\nOriginal Items Unchanged:')
console.log(arrayOfItems);
<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.25.0/ramda.js"></script>