I receive an observable containing an array of objects. I want to partition the array into two based on a property of each object. I'd like to do this in such a way that the resulting arrays are in observables that can use the async
pipe in the template, rather than having to manually manage subscriptions. I'm using RxJS 6 with Angular 9.
The trouble I'm having is in trying to partition based on individual elements while returning arrays to the destructuring assignment. I have tried variations of
public groupA: Observable<IItem[]>;
public groupB: Observable<IItem[]>;
...
// assigned values are not arrays
[this.groupA, this.groupB] = partition(
this.service.item$.pipe(
// split array into stream of its elements
concatAll(),
),
(item: IItem) => belongsInGroupA(item.myGroup)
);
and
[this.groupA, this.groupB] = partition(
this.service.item$,
(items: IItem[]) => {
return ???;
// How do I partition each item without first splitting into individual elements?
}
);
I know I could build the two arrays using something like map
(or, more appropriately, tap
), but I don't know how to auto-(un)subscribe using async
with that, if it's even possible.
partition
seems intended to divide streams of one item at a time, but my need feels like something well within the domain of Angular+RxJS and I just lack the understanding. Can you help?
tap
is that I loop through the source array once and build the two output arrays as I go. I can't stream each individual element to*ngFor
, and waiting until the arrays are built to useof()
orfrom()
seems foolish. In other words, I don't know howasync
applies. Am I missing something? – nshew13