typescript RxJS:Observable 数组的 Observable

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/47972512/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 05:08:16  来源:igfitidea点击:

RxJS: Observable of array for array of Observables

typescriptmergerxjsobservable

提问by EluciusFTW

I have the following situation using Observables in my Angular4 app that I just can't get to work: I want to gather summary data of all my booking days for a overview page. Getting all the days is an Observable, and each day has a list of bookings of that day that I have to retrieve - again an observable source. From this list I calculate a summary of the day. All these summaries I want to emit in a resulting observable.

我在我的 Angular4 应用程序中使用 Observables 时遇到以下情况,但我无法开始工作:我想为概览页面收集所有预订日的摘要数据。获取所有天数是一个 Observable,每天都有一个我必须检索的那天的预订列表 - 同样是一个可观察的来源。我从这个列表中计算出当天的摘要。我想在一个结果 observable 中发出所有这些摘要。

I have tried lot's of more complicated things, but always the inner observables where not waited on to complete and I got empty summaries. I have gotten back to the basics, and something along these lines should work:

我尝试了很多更复杂的事情,但总是没有等待完成的内部观察结果,我得到了空洞的总结。我已经回到了基础,按照这些思路应该可以工作:

getSummaries(): Observable<BookingDaySummary[]> {
    return this.bookingService.getBookingDays().take(1).mergeMap(
        days => this.calculateSummaryOfDays(days)
    )
};

private calculateSummaryOfDays(days: BookingDay[]): Observable<BookingDaySummary[]> {
    const summaries$ = days.map(day => this.calculateSummary(day));
    // I'm aware that the next line is not correct. 
    // Essentially I want the array of observables to complete 
    // and have an array of the resulting values.
    return Observable.merge(summaries$);
}

private calculateSummary(day: BookingDay): Observable<BookingDaySummary> {
    // ... logic to get summary from one day
}

However, the type of summaries$is Observable<Observable<BookingDaySummary>and not Observable. So it all boils down to: How do I make an Observable<T[]>from [Observable<T>]?

但是,类型summaries$isObservable<Observable<BookingDaySummary>而不是 Observable。所以这一切都归结为:我如何制作Observable<T[]>from [Observable<T>]

Also: Should the most inner method I use in .mapreturn an Observable or just be a map on the incoming type to T when intending to produce an Observable<T>?

另外:.map在打算生成Observable<T>?

回答by Paul John Leonard

How do I make an Observable< T[] > from Observable< T >[] ?

如何从 Observable< T >[] 制作 Observable< T[] > ?

forkJoin does this. Using String for your T:

forkJoin 就是这样做的。为您的 T 使用字符串:

import { of, forkJoin} from 'rxjs';
import { Observable } from 'rxjs'

const source:Array<Observable<String>> = [of('A'), of('B'), of('C') ];

const source2:Observable<String[]> = forkJoin(source);

source2.subscribe((res=>console.log(res)));

https://stackblitz.com/edit/arrayofobs2obsofarray?file=index.ts

https://stackblitz.com/edit/arrayofobs2obsofarray?file=index.ts

回答by Richard Johnson

Use the RxJS combineLateststatic function on your last statement of calculateSummaryOfDays:

combineLatest在您的最后一条语句中使用 RxJS静态函数calculateSummaryOfDays

return Observable.combineLatest(summaries$);

Note that your summaries$is an array of observables:

请注意,您summaries$是一个可观察的数组:

const summaries$: Observable<BookingDaySummary>[] = ...

From the combineLatestdocumentation:

combineLatest文档

Combines multiple Observables to create an Observable whose values are calculated from the latest values of each of its input Observables.

Static version of combineLatestaccepts either an array of Observables or each Observable can be put directly as an argument.

组合多个 Observable 以创建一个 Observable,其值是根据每个输入 Observable 的最新值计算得出的。

的静态版本combineLatest接受一个 Observable 数组,或者每个 Observable 都可以直接作为参数。

Thus this function will take your array of observables and return an observable of an array.

因此,此函数将获取您的 observable 数组并返回数组的 observable。

回答by Fan Cheung

try

尝试

return Observable.from(summaries$).mergeMap(value=>value);

I suppose summaries is an array of observable - Observable[] use Observable.from will emit them one by one and followed by mergeMap to flatten and execute the Observable and in the end you will get plain value emission.

我想 summaries 是一个 observable 数组 - Observable[] 使用 Observable.from 将一个一个地发出它们,然后使用 mergeMap 来展平并执行 Observable,最后你会得到纯值发射。