我是反应式编程的新手,有时候有点复杂。
给出服务事件列表[]
活动看起来像是
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魔术在一个简单的声明中对其进行分组和过滤吗?
答案 0 :(得分:0)
欢迎来到Observables的精彩世界。
您可以使用ForkJoin将多个Observable组合成一个:
请在此处查看此链接: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));