Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/398.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript rxjs switchmap observable,保存变量以供最终_Javascript_Rxjs - Fatal编程技术网

Javascript rxjs switchmap observable,保存变量以供最终

Javascript rxjs switchmap observable,保存变量以供最终,javascript,rxjs,Javascript,Rxjs,我需要知道解决这个问题的好方法 我正在使用switchMaps调用三个服务concat,但我需要一个变量first service for the last service。这样做的最佳实践是什么 例如: this.session.account.get() .switchMap(account => this.employerApi.get(account.accountId)) .switchMap(employer => this.addressApi.get(employer

我需要知道解决这个问题的好方法

我正在使用switchMaps调用三个服务concat,但我需要一个变量first service for the last service。这样做的最佳实践是什么

例如:

this.session.account.get()
.switchMap(account => this.employerApi.get(account.accountId))
.switchMap(employer => this.addressApi.get(employer.addressId))
.filter(address => address.numer % 2)
.subscribe(address => console.log(¿¿¿¿¿account.name?????, address.name));

感谢您的帮助

最简单的方法是边走边聚合值:

this.session.account.get()
  .switchMap(account =>
    this.employerApi.get(account.accountId).map(employer => ({employer, account}))
  .switchMap(data => 
    this.addressApi.get(employer.addressId).map(address => ({...data, address}))
  .filter(data => data.address.number % 2)
  .subscribe(...)

不确定这是最好的方法,但这里有一种方法:

this.session.account.get()
.switchMap(account => zip(Observable.from(account), this.employerApi.get(account.accountId))
.switchMap((account, employer) => zip(Observable.from(account), this.addressApi.get(employer.addressId))
.filter((account,address) => address.numer % 2)
.subscribe((account,address) => console.log(¿¿¿¿¿account.name?????, address.name));

然而,就最佳实践而言,我认为您需要的是后端的序列化程序,它以json格式返回结果,比如:
{“account”:account\u object,“address”:address\u object}

我认为使用zip函数不是很优雅,我更喜欢构建json。谢谢我相信这可能是最好的方法,谢谢!