我有两个从不同API端点填充的Observable数组。我想做的是根据ID比较它们,因此仅取相等的位置,然后从数组中的每个对象中挑选属性以产生一个新的Observable数组。有什么想法吗?
答案 0 :(得分:1)
这应该做:
forkJoin(
obs1$,
obs2$,
).pipe(
map((obs1, obs2) =>
obs1
.filter(v1 => obs2.some(v2 => v1.id === v2.id))
.map((v, i, a) => ({ ...v, ...(obs2[i] || {})}))
),
);
概念证明:
const d1 = [
{ id: 1, value: 'v1.1' },
{ id: 2, value: 'v1.2' },
{ id: 3, value: 'v1.3' },
{ id: 4, value: 'v1.4' },
];
const d2 = [
{ id: 1, value: 'v1.1 overriden' },
{ id: 2, value2: 'v1.2 extended' },
];
console.log(d1
.filter(v1 => d2.some(v2 => v1.id === v2.id))
.map((v, i, a) => ({ ...v, ...(d2[i] || {})}))
);
答案 1 :(得分:0)
forkJoin(
this.MeasuringPoints$,
this.LatestReadings$,
).subscribe( data => {
.filter(v1 => data[1].some(v2 => v1.uuid === v2.mpUuid))
.map((v, i) => ({ ...v, ...(data[1][i] || {})}));
});