ramda.js中有没有等价于mapValues的函数(类似于lodash)?
Is there any function equivalent to mapValues in ramda.js (similar to lodash)?
我正在使用 ramdajs in my application. I have to use a utility similar to mapValues 的 lodash。 ramdajs 中是否已有我可以使用的函数。如果没有,我如何用 ramda 中的其他函数实现它? (显然我可以使用 nativejs 来实现这个但我想使用 ramdajs)
是的,只是 map
。
map
对任何 Functor 进行操作,Ramda 提供数组、对象和函数的实现,所有这些都是仿函数,并委托给其他类型的 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>
我认为 mapObjIndexed
可以做与 mapValues
类似的事情,但没有迭代速记。
const users = {
fred: { user: 'fred', age: 40 },
pebbles: { user: 'pebbles', age: 1 }
};
R.mapObjIndexed((value, key) => value.age, users)
输出:
{"fred": 40, "pebbles": 1}
我正在使用 ramdajs in my application. I have to use a utility similar to mapValues 的 lodash。 ramdajs 中是否已有我可以使用的函数。如果没有,我如何用 ramda 中的其他函数实现它? (显然我可以使用 nativejs 来实现这个但我想使用 ramdajs)
是的,只是 map
。
map
对任何 Functor 进行操作,Ramda 提供数组、对象和函数的实现,所有这些都是仿函数,并委托给其他类型的 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>
我认为 mapObjIndexed
可以做与 mapValues
类似的事情,但没有迭代速记。
const users = {
fred: { user: 'fred', age: 40 },
pebbles: { user: 'pebbles', age: 1 }
};
R.mapObjIndexed((value, key) => value.age, users)
输出:
{"fred": 40, "pebbles": 1}