I’m working in an angular 6 application. I have a function that returns an observable of an array of observables, which in turn are observables of arrays.
The results might look something like this:
[
[],
[],
[object, object],
[],
[object],
[],
[object, object, object],
[]
]
As you can see, it’s an array of arrays and some arrays might be empty.
I’d like to list each object in each inner array on the page as follows:
<div *ngFor="let watcher of InvitationService.getInvitations() | async">
<div *ngFor="let invitation of watcher">
{{ invitation | json }}
</div>
</div>
And getInvitations() looks like this:
getInvitations() {
const groupedInvitations$: Observable<any>[] = [];
groups.forEach(group => {
const groupInvites$ = this._firestoreService.collection(`GroupInvitations/${group.groupID}/Invites`);
groupedInvitations$.push(groupInvites$);
});
Return Observable.combineLatest(groupedInvitations$);
}
What I’d like to do is remove the empty arrays from the results returned from combineLatest(…) and flatten the rest into a single one dimensional array.
I understand one can do this by piping through map(…) or flatMap(…) or things like that. But I’ve been trying things like that and nothing’s been working.
For example, I’ve tried this:
Return Observable.combineLatest(groupedInvitations$).pipe(
map(item => {
console.log(item);
});
);
But nothing gets logged to the console.
How can I flatten the array and remove empty arrays? Thanks.