TLTR: Look at the javascript snippet.
I have an array of items.
I want to map items and change their structure.
I need to keep some properties and I also need to set a new property. However, the new property value is going to be based on the item that I am currently mapping.
// 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>