【发布时间】:2018-11-08 12:59:04
【问题描述】:
所以,我有服务器,它根据请求返回对象数组
例子:
[
{
id: 1
name: name1
},
{
id: 2
name: name2
}
]
数组可以有无限个元素;
之后,我必须为数组中的每个元素发送新的请求,并带有响应 ID。
示例:
https://localhost/api/parent/${id}/child
回复:
[
{
childId: 1
name: name1
},
{
childId: 2
name: name2
},
{
childId: 3
name: name3
}
]
然后我必须为每个新请求的响应发送新请求
示例:
https://localhost/api/parent/${id}/child/${childId}/grandChild
回复:
[
{
grandChildId: 1
name: name1
},
{
grandChildId: 2
name: name2
},
{
grandChildId: 3
name: name3
}
]
反应和其他人一样
然后我必须组织数据看起来像这样
[
{
id: 1
name: name1
children: [
{
childId: 1
name: name1,
grandChildren: [
{
grandChildId: 1
name: name1
},
{
grandChildId: 2
name: name2
},
{
grandChildId: 3
name: name3
}
]
},
{
childId: 2
name: name2
},
{
childId: 3
name: name3
}
]
},
{
id: 2
name: name2
}
]
每个父母都有一组孩子,每个孩子都有一组孙子。
这是我的尝试,但如果我尝试在组件中执行 parents.forEach(parent => console.log(parent.childern)) 它是未定义的
getData() {
return this.http.get<Parent[]>(baseUrl + 'parent', {withCredentials: true}).pipe(
map(parents => {
parents.forEach(parent => {
this.getChildren(parent).subscribe(Children => {
parent.Children = Children;
parent.Children.forEach(Children => {
this.getGrandChildren(Children).subscribe(grandChild => {
Children.grandChildren = grandChild;
});
});
});
});
return parents;
})
);
}
getChildren(parent: Parent): Observable<Child[]> {
return this.http.get<Children[]>(baseUrl + `parent/${parent.id}/children`, {withCredentials: true});
}
getGrandChildren(child: Child): Observable<GrandChild[]> {
return this.http.get<GrandChild[]>(baseUrl + `children/${child.id}/GrandChildren`, {withCredentials: true});
}
【问题讨论】:
标签: angular rxjs observable angular6