这是复杂情况的简化,其中,如果值无效,则可以过滤数组中的某些可观察对象。问题在于,过滤后的可观察对象不允许其他对象完成合并。哪种运营商或方法可以处理这种情况,从而允许在订阅中记录有效数据?
// RxJS v6+
import { fromEvent,combineLatest,of } from 'rxjs';
import { mapTo,startWith,scan,tap,map,filter } from 'rxjs/operators';
const userData$ = [
of({ name: 'Joseph',age: 23}),of({ name: 'Mario',age: 33}),of({ name: 'Robert',age: 24}),of({ name: 'Alonso',age: 25})
];
const names = ['Joseph','Mario','Robert','Alonso'];
combineLatest(
names.map((name,i) => {
return userData$[i].pipe(
map(({name,age})=> { return{ name,age: age * 2} }),filter(({age}) => age < 67),map(({name,age})=> { return{ name: name.toLocaleUpperCase(),age} }),)
})
)
.pipe(
tap(console.log),)
.subscribe();
如果将值更改为67,则所有可观察对象将显示数据。