我有一个 struct 如下的对象:
{
parent1: [
{childId: 1},
{childId: 2}
],
parent2: [
{childId: 3},
{childId: 4}
],
parent3: [
{childId: 5},
{childId: 6}
]
}
以下服务:
addFamily(data: any): Observable<Family> {
const body = JSON.stringify(data);
return this.httpClient
.post<Family>(this.apiUrl + '/family', body, this.httpOptions)
}
addParent(data: any): Observable<Parent> {
const body = JSON.stringify(data);
return this.httpClient
.post<Parent>(this.apiUrl + '/parent', body, this.httpOptions)
}
addChild(data: any): Observable<Child> {
const body = JSON.stringify(data);
return this.httpClient
.post<Child>(this.apiUrl + '/child', body, this.httpOptions)
}
数据库中有相应的"family"、"parent"和"child"表,我想使用RxJS高阶映射运算符相应地发布到这些表中的每一个,以根据以前的调用构建数据响应.这个 idea 是:
- 拨打
addFamily()
并返回新的familyId
- 通过
familyId
呼叫addParent()
并返回新的parentId
- for each 创建的子项调用
addChild()
传入parentId
对示例对象执行操作后,将出现:
- 添加了6个子元素(FK child\u parent)
- 添加3个家长(FK parent_family)
- 添加了1个系列
目前,代码库正在使用多个嵌套的订阅来执行上述任务,这就是为什么我研究RxJS并从类似的question个代码中偶然发现了下面的代码.
private getData(): Observable<VmData> {
return this.service.getSomeData().pipe(
switchMap(session => this.service.getUserData(session.userId).pipe(
switchMap(user => this.service.getMetaData(user.id).pipe(
// by nesting, map has access to prior responses
map(userMeta => this.buildVmData(session, user, userMeta))
))
)),
tap(() => this.isLoading = false)
);
}
如果是一个家庭,上述代码将起作用->;单亲->;一个子元素,但我如何修改它以添加一个家庭,循环遍历每个家长,并在该家长下添加每个子元素?非常感谢您的帮助.非常感谢.