简体   繁体   English

ramda.js中是否有任何等效于mapValues的函数(类似于lodash)?

[英]Is there any function equivalent to mapValues in ramda.js (similar to lodash)?

I'm using ramdajs in my application. 我在应用程序中使用ramdajs I have to use a utility similar to mapValues of lodash. 我必须使用类似于lodash的mapValues的实用程序。 Is there already a function in ramdajs which I can use. 我已经可以使用ramdajs中的功能了。 If not, how can I implement that with other functions in ramda? 如果没有,如何在ramda中使用其他功能来实现呢? (Obviously I can use nativejs to implement this but I want to use ramdajs) (显然,我可以使用nativejs来实现这一点,但我想使用ramdajs)

Yes, it's just map . 是的,它只是map

map operates on any Functor , and Ramda supplies the implementations for arrays, objects, and functions, all of which are functors, and delegates to map methods for other types. map在任何Functor map运行,Ramda提供数组,对象和函数的实现,它们都是函子,并为其他类型的map方法提供委托。

So you can just use map : 所以你可以只使用map

 const square = n => n * n console .log ( map (square, {a: 1, b: 2, c: 3}) //=> {a: 1, b: 4, c: 9} ) console .log ( map (toUpper, {x: 'foo', y: 'bar'}) //=> {x: 'FOO', y: 'BAR'} ) 
 <script src="//cdnjs.cloudflare.com/ajax/libs/ramda/0.26.1/ramda.js"></script><script> const {map, toUpper} = R </script> 

I think mapObjIndexed could do the similar things as mapValues but without the iteratee shorthands. 我认为mapObjIndexed可以做与mapValues类似的事情,但是没有iteratee的简写形式。

const users = {
   fred:    { user: 'fred', age: 40 },
   pebbles: { user: 'pebbles', age: 1 }
};
R.mapObjIndexed((value, key) => value.age, users)

which outputs: 输出:

{"fred": 40, "pebbles": 1}

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

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