我有两个接口,分别是 Team 和 Company
public interface Team {
id: number;
name: string;
companyId: number;
}
public interface Company {
id: number;
name: string;
}
这是样本数据:
"companies": [
{
"id": 3,
"name": "XSoftware",
"location": "Nagar"
},
{
"id": 5,
"name": "Google",
"location": "Seattle"
},
{
"id": 7,
"name": "YS",
"location": "Dhanmondi"
},
{
"id": 8,
"name": "Amazon",
"location": "Seattle DC"
},
{
"name": "ToTD",
"location": "Pink City",
"id": 10
}
]
"teams": [
{
"id": 1,
"name": "Team X",
"expertise": "Java",
"companyId": 3
},
{
"id": 2,
"name": "Team Y",
"expertise": "Angular",
"companyId": 3
},
{
"id": 3,
"name": "Team Z",
"expertise": "Spring Boot",
"companyId": 8
},
{
"id": 4,
"name": "Team M",
"expertise": "Node Js",
"companyId": 5
}
]
所以我想根据companyId将company作为财产分配给每个团队。像这样:
"teams": [
{
"id": 1,
"name": "Team X",
"expertise": "Java",
"companyId": 3,
"company": {
"id": 3,
"name": "XSoftware",
"location": "Nagar"
}
},
{
"id": 2,
"name": "Team Y",
"expertise": "Angular",
"companyId": 3,
"company": {
"id": 3,
"name": "XSoftware",
"location": "Nagar"
}
},
{
"id": 3,
"name": "Team Z",
"expertise": "Spring Boot",
"companyId": 8,
"company": {
"id": 8,
"name": "Amazon",
"location": "Seattle DC"
}
},
{
"id": 4,
"name": "Team M",
"expertise": "Node Js",
"companyId": 5,
"company": {
"id": 5,
"name": "Google",
"location": "Seattle"
}
}
]
那么如何使用 RxJs 实现这一点。我有两个分别返回 Team[] 和 Company[] 的 Observable 的 observable。
const teams$: Observable<Team[]> = this.httpClient.get<Team[]>('/teams');
const companies$: Observable<Company[]> = this.httpClient.get<Company[]>('/companies');
那么,如何做到这一点?我知道它可以以命令式的方式完成(通过使用循环、if-else 等),但我想通过只使用反应式运算符、观察者来以反应式的方式做到这一点。