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

时间:2019-05-21 12:46:20

标签: javascript lodash ramda.js

我在应用程序中使用ramdajs。我必须使用类似于lodash的mapValues的实用程序。我已经可以使用ramdajs中的功能了。如果没有,如何在ramda中使用其他功能来实现呢? (显然,我可以使用nativejs来实现,但是我想使用ramdajs)

2 个答案:

答案 0 :(得分:3)

是的,只是map

map可在任何Functor上运行,Ramda提供数组,对象和函数的实现,它们都是函子,并委托其他类型的map方法。 / p>

因此您可以只使用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>

答案 1 :(得分:2)

我认为mapObjIndexed可以做与mapValues类似的事情,但是没有iteratee的简写。

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

输出:

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