I have
- An array of objects
- A function
permission(obj): Promise<boolean>
How can I filter this array by the promise?
I've tried numerous things, and the closest I got was
of(arr).pipe(
switchMap(items =>
from(items)
.pipe(
mergeMap(item =>
fromPromise(permission(item)).pipe(
map(shown => ({show, item})),
filter(data => data.shown),
map(data => data.item)
)
))
));
But that just seems needlessly complicated.
I had hoped I could do it more simple like of(arr).pipe(filterByPromise(permission))
, but can't seem to figure out how.
I made a Stackblitz https://stackblitz.com/edit/rxjs-g1u8yk
StackBlitz code
import { of } from 'rxjs';
import { map, filter } from 'rxjs/operators';
// Mocked 3rd party function
function visible(obj): Promise<boolean> {
return new Promise(resolve => {
const shouldShow = obj.shouldShow < 30;
resolve(shouldShow);
});
}
const arr = [{
shouldShow: 39,
data1: 'abc',
data2: 'bcd'
}, {
shouldShow: 22,
data1: 'cde',
data2: 'def'
}];
of(arr).pipe(
filter(obj => visible(obj))
).subscribe(result => console.log(result));