I'm trying to map observables by filtering them based on events emitted from other observables. So here's my challenge:
How do I feed a property of an event to a filter?
Here's an example with which entities I want to get. Currently it's working because I have hardcoded the relational IDs. I would like to read those IDs from observables mentioned in the comments.
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
);
}
}
And here is a live example: https://stackblitz.com/edit/angular-streamception?file=src%2Fapp%2Fapp.component.ts
I have tried using mergeMap to merge the first observable with the latter and filter from within, but it feels like working against the reactive way.
If I'm approaching the problem from a wrong angle, please do point me in the right direction.