流会保留以下对象
const data = [
{ type: 'gps', id: 1, val: 1 },
{ type: 'gps', id: 2, val: 2 },
{ type: 'speed', id: 2, val: 3 },
{ type: 'gps', id: 3, val: 4 },
{ type: 'speed', id: 4, val: 5 },
{ type: 'gps', id: 4, val: 6 },
{ type: 'gps', id: 5, val: 7 }
]
如果id相同,则合并对象。如果没有id匹配,则忽略该对象:
[
[{type: 'gps', id:2, val:2}, { type: 'speed', id: 2, val: 3 }],
[{ type: 'speed', id: 4, val: 5 },{ type: 'gps', id: 4, val: 6 }]
]
我的想法是将具有相同类型的对象分组,最后得到两个新流
Rx.Observable.from(data)
.groupBy((x) => x.type)
.flatMap((g) => ...)
....
然后在id
相等的情况下再次合并/压缩它们。
我不确定如何在Rx中指定它,我也不确定这是否是一个好方法。
答案 0 :(得分:0)
无需拆分流并再次将其合并。您可以使用base
收集对象,scan
使用不符合条件的对象
filter

const data = [
{ type: 'gps', id: 1, val: 1 },
{ type: 'gps', id: 2, val: 2 },
{ type: 'speed', id: 2, val: 3 },
{ type: 'gps', id: 3, val: 4 },
{ type: 'speed', id: 4, val: 5 },
{ type: 'gps', id: 4, val: 6 },
{ type: 'gps', id: 5, val: 7 }
]
const generator$ = Rx.Observable.from(data)
generator$
.scan((acc, x) => {
if (R.contains(x.id, R.pluck('id', acc))) {
acc.push(x);
} else {
acc = [x]
}
return acc
}, [])
.filter(x => x.length > 1)
.subscribe(console.log)