假设我有两个可观察值。
第一个可观察到的是某些列表的数组:
[
{id: 'zzz', other props here...},
{id: 'aaa', ...},
{id: '007', ...}
... and more over time
]
第二个可观察对象是一组被忽略的列表:
[
{id: '007'}, // only id, no other props
{id: 'zzz'}
... and more over time
]
结果应该是列表的新可观察值(第一个可观察值),但不得包含任何被忽略的列表:
[
{id: 'aaa', other props here...}
... and more over time
]
这是我在发布之前所拥有的:
obs2.pipe(withLatestFrom(obs1, ? => ?, filter(?));
我没有测试过,但我认为应该没问题:
combineLatest(values$, excluded$).pipe(
map(([values, excluded]) => {
// put all the excluded IDs into a map for better perfs
const excludedIds: Map<string, undefined> = excluded.reduce(
(acc: Map<string, undefined>, item) => {
acc.set(item.id, undefined)
return acc;
},
new Map()
);
// filter the array, by looking up if the current
// item.id is in the excluded list or not
return values.filter(item => !excludedIds.has(item.id))
})
)
解释:
Using combineLatest
无论您从哪里获得更新,您都会收到警告。如果你使用withLatestFrom
正如您的示例中所示,只有在以下情况下才会触发更新values$
可观察到的已更新。但如果excluded$
更改,它不会触发您的情况的更新。
然后将所有排除的 ID 放入映射而不是数组中,因为我们需要知道是否应该排除给定的 ID。查看地图比查看数组要快得多。
然后只需过滤值数组即可。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)