繁体   English   中英

先进的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> 

我认为这可能是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([ Ro(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> 

我不是凭空想象就不是Ramda或FP的专家,但似乎最简单的方法是将常规函数传递到R.compose ,然后在pick属性之前将cells属性添加到每个对象上你要。

让我知道这是否适合您,或者这是否是反模式,并且您严格在寻找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> 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM