我试图通过基于从其他可观察者发出的事件过滤它们来映射可观察量。所以这就是我的挑战:
如何将事件的属性提供给过滤器?
以下是我想要获得的实体的示例。目前它正在工作,因为我已经硬编码了关系ID。我想从评论中提到的可观察量中读取这些ID。
export class AppComponent implements OnInit {
currentInspection$;
currentAircraft$;
currentConfiguration$;
ngOnInit() {
const aircraft$ = Observable.of([
{ id: 1, callsign: 'D-AISY', configurationId: 100 },
{ id: 2, callsign: 'D-OOBE', configurationId: 200 },
{ id: 3, callsign: 'D-OOOO', configurationId: 100 }
]);
const inspections$ = Observable.of([
{ id: 10, aircraftId: 3 },
{ id: 20, aircraftId: 2 }
]);
const configurations$ = Observable.of([
{ id: 100, name: 'Default CRJ900 configuration' },
{ id: 200, name: 'Default B737 configuration' }
]);
this.currentInspection$ = inspections$.pipe(
map(inspections => inspections.find(inspection => inspection.id === 20))
);
// How do I get id property of this.currentInspection$ instead of 1 here?
this.currentAircraft$ = aircraft$.pipe(
map(aircraft => aircraft.filter(a => a.id === 1)) // I want the "1" to not be hardcoded
);
// How do I get configurationId property of this.currentAircraft$ instead of 100 here?
this.currentConfiguration$ = configurations$.pipe(
map(configurations => configurations.filter(c => c.id === 100)) // I want the 100 to not be hardcoded
);
}
}
这是一个实例: https://stackblitz.com/edit/angular-streamception?file=src%2Fapp%2Fapp.component.ts
我已经尝试使用mergeMap将第一个observable与后者进行合并并从内部进行过滤,但感觉就像反对反应方式。
如果我从错误的角度接近问题,请指出正确的方向。