TLTR:查看javascript代码段。
我有一系列物品。
我想映射项目并更改其结构。
我需要保留一些属性,还需要设置一个新属性。但是,新的属性值将基于我当前正在映射的项目。
void replacee(vector<vector<char>> &vec, char oldd, char neww) {
for(vector<char> &v:vec) {
for(char &c:v) {
if (c == oldd) { c = neww; }
}
}
}
// 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)
答案 0 :(得分:2)
我认为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>
答案 1 :(得分:1)
无论如何,我都不是Ramda或FP的专家,但似乎最简单的方法是将常规函数传递到R.compose
中,该函数将cell属性添加到每个对象上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>