javascript - 参考对象的可观察链
问题描述
我是 Observables 的新手,我不确定我是否做对了。
这是场景。
a) 订阅路由参数
b)当参数更改时,执行服务请求以获取可观察的数据
c) 处理数据
d) 然后对它们的依赖项进行更多请求并获取它们的依赖项 observables。
我的问题是,在 d) 之后,我可以很好地获取他们的依赖项 observables,但我没有到已处理数据的映射以根据需要附加响应。
例子:
this.route.queryParams.pipe(map((params) => {
this.selectedTabIndex = +params['tabIndex'];
return params
})).pipe(switchMap((params) => {
return this.eventService.getEvent(params['eventID']);
})).pipe(map((event) => {
this.event = event;
this.selectedActivities = event.getActivities();
return this.selectedActivities;
})).pipe(mergeMap((activities) => {
return combineLatest(activities.map((activity) => {
return this.eventService.getStreams(this.event.getID(), activity.getID(), ['Latitude', 'Longitude'])
}))
})).pipe(map((activityStreams) => {
// Here is my problem
// As you can see I can get well the streams for the above activities
// But I don't have the activity that the streams belong to to attach them to
debugger
})).subscribe()
因此,在最后一个管道中,结果如下所示:
但是正如您所看到的,我没有对这些流数组最终应该附加到的活动的引用。这就是我想要做的。
也许我做的事情完全错误,所以我愿意讨论。
解决方案
在 Sheshaj Awasthi 的帮助下
我做了以下事情:
this.route.queryParams.pipe(map((params) => {
this.selectedTabIndex = +params['tabIndex'];
return params
})).pipe(switchMap((params) => {
return this.eventService.getEvent(params['eventID']);
})).pipe(map((event) => {
this.event = event;
this.selectedActivities = event.getActivities();
return this.selectedActivities;
})).pipe(map((activities) => {
return activities.reduce((activityStreamPairArray, activity) => {
activityStreamPairArray.push({
activity: activity,
activityStreams: this.eventService.getStreams(this.event.getID(), activity.getID(), ['Latitude', 'Longitude']),
});
return activityStreamPairArray
}, [])
})).pipe(mergeMap((activityStreamPairArray) => {
return combineLatest(activityStreamPairArray.reduce((flattenedArray, activityStreamPair) => {
flattenedArray.push(of(activityStreamPair.activity), activityStreamPair.activityStreams);
return flattenedArray
}, []))
})).pipe(map((test) => {
debugger
}))
我现在有
它由一个数组组成,其中每 2 个元素可以组合。
推荐阅读
- javascript - Price.map 不是函数
- concurrency - V-lang:如何每秒发送 +2500 个 HTTP 请求?
- reactjs - 如何在 React js 中使用数组将数组推送到对象中?
- javascript - 嵌入对象以显示 PDF 显示为不可见
- python - 无法导入类,即使快速修复找到它
- javascript - 修复 JavaScript LoadMore 按钮以每页加载 5 个项目 - Django
- python - 使用自生成键将嵌套列表转换为字典
- reactjs - React Docusign Clickwrap 凭证
- linux - 无法 docker-compose 任何项目
- ruby-on-rails - 自定义验证器::创建不在 Rails 应用程序上运行