在 Ramda 中如何查看数组中的下一个值,以删除一个接一个重复的值
In Ramda how to look at next value in an array, to remove values that repeat one after another
所以我有以下数组
[true,true,false,false,false,true,false,false,true,true].
如何使用 ramda 使数组如下所示
[true,false,true,false,true]
另外,如果数据是
[{t: '2018-10/09', v:true},{t: '2018-10/08', v:true}, {t: '2018-10/07', v:false},{t: '2018-10/06', v:false},{t: '2018-10/05', v:false},{t: '2018-10/04', v:true},{t: '2018-10/03', v:false},{t: '2018-10/02', v:false},{t: '2018-10/01', v:true},{t: '2018-09/09', v:true}]
我只想使用 v 删除重复,那么我会使用 R.dropRepeats(bool.v) 吗?
另请注意,我正在使用 R.pipe 获取输入并对数据进行转换。
我建议使用 groupWith
的解决方案:
您使用 groupWith
将连续的相同元素分组到子数组中,然后您在 map
中使用 head
来获取每个子数组的第一个元素。
const prune = R.pipe(R.groupWith(R.equals), R.map(R.head));
console.log(
prune([true,true,false,false,false,true,false,false,true,true]));
<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.25.0/ramda.js"></script>
Ramda 恰好有一个内置函数,dropRepeats
const bools = [true,true,false,false,false,true,false,false,true,true]
console.log(R.dropRepeats(bools))
<script src="//cdnjs.cloudflare.com/ajax/libs/ramda/0.25.0/ramda.js"></script>
更新
有评论问我如何在稍微复杂的结构上执行此操作。使用 dropRepeatsWith
and eqProps
会很相似:
const bools = [{t: '2018-10/09', v:true},{t: '2018-10/08', v:true}, {t: '2018-10/07', v:false},{t: '2018-10/06', v:false},{t: '2018-10/05', v:false},{t: '2018-10/04', v:true},{t: '2018-10/03', v:false},{t: '2018-10/02', v:false},{t: '2018-10/01', v:true},{t: '2018-09/09', v:true}]
console.log(R.dropRepeatsWith(R.eqProps('v'), bools))
<script src="//cdnjs.cloudflare.com/ajax/libs/ramda/0.25.0/ramda.js"></script>
所以我有以下数组
[true,true,false,false,false,true,false,false,true,true].
如何使用 ramda 使数组如下所示
[true,false,true,false,true]
另外,如果数据是
[{t: '2018-10/09', v:true},{t: '2018-10/08', v:true}, {t: '2018-10/07', v:false},{t: '2018-10/06', v:false},{t: '2018-10/05', v:false},{t: '2018-10/04', v:true},{t: '2018-10/03', v:false},{t: '2018-10/02', v:false},{t: '2018-10/01', v:true},{t: '2018-09/09', v:true}]
我只想使用 v 删除重复,那么我会使用 R.dropRepeats(bool.v) 吗? 另请注意,我正在使用 R.pipe 获取输入并对数据进行转换。
我建议使用 groupWith
的解决方案:
您使用 groupWith
将连续的相同元素分组到子数组中,然后您在 map
中使用 head
来获取每个子数组的第一个元素。
const prune = R.pipe(R.groupWith(R.equals), R.map(R.head));
console.log(
prune([true,true,false,false,false,true,false,false,true,true]));
<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.25.0/ramda.js"></script>
Ramda 恰好有一个内置函数,dropRepeats
const bools = [true,true,false,false,false,true,false,false,true,true]
console.log(R.dropRepeats(bools))
<script src="//cdnjs.cloudflare.com/ajax/libs/ramda/0.25.0/ramda.js"></script>
更新
有评论问我如何在稍微复杂的结构上执行此操作。使用 dropRepeatsWith
and eqProps
会很相似:
const bools = [{t: '2018-10/09', v:true},{t: '2018-10/08', v:true}, {t: '2018-10/07', v:false},{t: '2018-10/06', v:false},{t: '2018-10/05', v:false},{t: '2018-10/04', v:true},{t: '2018-10/03', v:false},{t: '2018-10/02', v:false},{t: '2018-10/01', v:true},{t: '2018-09/09', v:true}]
console.log(R.dropRepeatsWith(R.eqProps('v'), bools))
<script src="//cdnjs.cloudflare.com/ajax/libs/ramda/0.25.0/ramda.js"></script>