Suppose I have an Observable of items
let items$ = Observable<Item[]>
and each Item has a property isSelected$, which itself is an Observable:
private isSelected$: Observable<boolean>
What would be the best way to receive a list of all items that are currently selected? My solution doesn't look quite right:
items$.pipe(
switchMap(items =>
combineLatest(items.map(item =>
item.isSelected$.pipe(
map(isSelected => ({item: item, isSelected: isSelected})))),
map(items => items.filter(item => item.isSelected).map(item => item.item))
)))
It works and I have used it before, but it's such a complicated construct for something I have to do quite often. There must be a better way.
Note: The subscriber needs a list of all the selected items, not a stream of individual items.
Example:
items$ would emit the following list:
[{id: 1, isSelected$: of(true)},
{id: 2, isSelected$: of(false)},
{id:3, isSelected$: of(true)}]
And as a result in our subscribe, we would like to get the list:
[{id: 1}, {id: 3}]