I have an Observable where User has an array property Posts[] that I want to iterate through, invoke a method with the intermediate values and then return an Observable with the resulting objects as arrays.
At the moment I have the following code that works:
public ngOnInit() {
const results: any[] = [];
this.controlData$ = this.controlContent$
.pipe(
filter((input) => input !== null),
flatMap((p: IControlContentSection) => p.controlPerformers),
map((m: IControlPerformer) => {
results.push({
...m,
nextOccurrence: this.getNextControlDate(m.controlFrequency, m.firstOccurrence),
});
return results;
}),
takeUntil(this.destroy$),
);
}
I use an array variable results: any[], but I do not like this solution as it relies on this external variable, making it working only when the component is initialized.
I tried to use toArray() or a reduce((x, y) => x.concat(y), []) after the map, but this then resolves into null in the template.
How can I return an Observable without the need of an external variable?
In template I subscribe via async pipe:
<div *ngIf="controlData$ | async as controllers">
...
<div *ngFor="let ctrl of controllers; trackBy:ctrl?.userId">
...
</div>
</div>