本文介绍了对于数组中的重复值,如何使用“重复"?使用 Ramda.js的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用 count:[1, 2, 3]
复制 target:[ "a", "b", "c"]
I am trying to duplicate target:[ "a", "b", "c"]
with count:[1, 2, 3]
所需的输出是[a"、b"、b"、c"、c"、c"]
它不适用于此代码:
const fn = ({ target, count }) => R.map (R.repeat (target, count))
const Data = { target : ["a", "b", "c"], count : [1, 2, 3] }
const result = fn(Data)
我正在尝试使用 Ramda.js 寻找解决方案.
I am trying to find solution with Ramda.js.
谢谢.
推荐答案
另一个相当简单的解决方案:
Another fairly simple solution:
const fn = ({target, count}) =>
unnest (zipWith (repeat) (target, count))
const data = {
target: ['a', 'b', 'c'],
count: [1, 2, 3]
}
console .log (fn (data))
<script src="//cdnjs.cloudflare.com/ajax/libs/ramda/0.27.1/ramda.min.js"></script>
<script> const {unnest, zipWith, repeat} = R </script>
target
和 data
的无点函数很简单:
A point-free function of target
and data
is easy enough:
compose (unnest, zipWith (repeat))
如果它们被包裹在一个对象中并且你真的想要无点,那么来自 Hitmands 的答案似乎是最好的,或者使用这种技术的变体:
If they are wrapped in an object and you really want point-free, then the answer from Hitmands seems best, or a variant using this technique:
compose (unnest, apply (zipWith (repeat)), props (['target', 'count']))
这篇关于对于数组中的重复值,如何使用“重复"?使用 Ramda.js的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!