RxJS可观察的过滤和分组
问题描述:
我是新来的反应式编程,它有时候有点复杂。RxJS可观察的过滤和分组
从服务事件[]
事件看起来给出一个列表就像
Event:
{
beginDate: Date,
name: string,
type: State
}
State:
{
PENDING, FINISHED, UPCOMING
}
我想组这在以下结构
GroupedByMonthEvent:
{
monthYear: Date,
event: Event
}
在命令行式风格我把它写:
allEvents$: Observable<Tournament[]>;
allEventsGrouped$: Observable<GroupedByMonthEvent[]>;
...
this.allEvents$ = this.store.select(state => state.events);
this.allEventsGrouped$ = this.allEvents$.map(t => {
return _.chain(t).sortBy(function (value) {
return new Date(value.beginDate);
})
.groupBy(function (event) {
return moment(event.beginDate).format('MMMM YYYY');
}).toPairs()
.map(function (events: Event[]) {
return _.zipObject(['monthYear', 'events'], events);
})
.value();
});
接下来我要过滤该Observerable。 我试图.map和.filter该集合,但我不明白。 现在我只订阅Observerable并过滤它过时。
有人可以帮助RxJS魔术组合并过滤它在一个简单的声明?
答
欢迎来到观察者的美妙世界。
您可以使用多个ForkJoin观测量组合成一个:
请参阅此链接在这里:https://www.learnrxjs.io/operators/combination/forkjoin.html
片段:
/*
when all observables complete, give the last
emitted value from each as an array
*/
const example = Rx.Observable.forkJoin(
//emit 'Hello' immediately
Rx.Observable.of('Hello'),
//emit 'World' after 1 second
Rx.Observable.of('World').delay(1000),
//emit 0 after 1 second
Rx.Observable.interval(1000).take(1),
//emit 0...1 in 1 second interval
Rx.Observable.interval(1000).take(2),
//promise that resolves to 'Promise Resolved' after 5 seconds
myPromise('RESULT')
);
//output: ["Hello", "World", 0, 1, "Promise Resolved: RESULT"]
const subscribe = example.subscribe(val => console.log(val));