简体   繁体   中英

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

I'm using ramdajs in my application. I have to use a utility similar to mapValues of lodash. Is there already a function in ramdajs which I can use. If not, how can I implement that with other functions in ramda? (Obviously I can use nativejs to implement this but I want to use ramdajs)

Yes, it's just 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.

So you can just use 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.

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}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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