ramda.js - 对于数组中的重复值,如何在 Ramda.js 中使用“重复”
问题描述
我正在尝试target:[ "a", "b", "c"]
复制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 找到解决方案。
谢谢。
解决方案
另一个相当简单的解决方案:
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
很简单:
compose (unnest, zipWith (repeat))
如果它们被包裹在一个对象中并且你真的想要无点,那么来自 Hitmands 的答案似乎是最好的,或者使用这种技术的变体:
compose (unnest, apply (zipWith (repeat)), props (['target', 'count']))
推荐阅读
- javascript - Facebook 分享按钮当前 URL
- excel - Excel - 如果多个非连续列包含值是,我想返回真值。但我也希望它忽略空白单元格
- vb.net - 如何在我的 vb 代码中将 psiphon-tunnel-core.exe 作为后台进程运行?
- python - 如何(正确)在 Kubernetes 上部署 MongoDB 并从另一个 Pod/Job 访问它?
- javascript - 如何仅使用 Pure JS 而不是使用任何库或框架在分页中添加分页数字之间的点?
- image-processing - 是否可以在不将图像渲染到屏幕的情况下收集 pygame 游戏板的图像矩阵?
- postgresql - Can psql output a description of all tables, but only tables?
- latex - 一个接一个地显示向量的条目
- javascript - 如何设置条件以从 Javascript 对象中删除属性?
- python-3.x - python :- IndexError: 将列表转换为字典时列表索引超出范围