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>