如何从RxJs中的flatMap返回其他值 [英] How to return additional values from flatMap in RxJs

查看:201
本文介绍了如何从RxJs中的flatMap返回其他值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在以下设置中,我需要从flatMap返回除Observable,key字符串以外的其他值:

I need to return an additional value other than Observable, key string from flatMap from below setup:

this.subscribeInit = this.tradingService.getInProgress()
.pipe(flatMap((s) => {
    this.keys = s.map((tdi) => tdi.key);
    return this.keys;
}),
flatMap((key) =>{
    var res = this.tradingService.getTradingData(key);//returns `Observable<any>`
    return res;
}))
.subscribe(res => {
    console.log('Key is : ' + res.key);
}, 
undefined,
() => this.onComplete());

类似的东西:

flatMap((key) =>{
    var res = this.tradingService.getTradingData(key);
    return {res,key};
}))

如何实现?

推荐答案

只需使用地图...

flatMap((key) =>{
    return this.tradingService.getTradingData(key).pipe(map((res) => {
        return {res,key};
    }));
}))

ps:我有点困惑:不推荐使用flatMap并替换我的mergeMap,switchMap,...吗?

ps: i am a bit confused: isnt flatMap deprecated and replaced my mergeMap, switchMap, ... ?

显然,问题是由使用flatMap而不是mergeMap引起的.

edit: apparently the problem was caused by using flatMap instead of mergeMap.

这篇关于如何从RxJs中的flatMap返回其他值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆