如何将响应对象属性从中的数组转换为请求rxjs



neneneba API返回一个JSON对象数组。

[
{
"vacancy": "61a6597b0dc105d6e79a1f30",
"createdBy": "61aa11644afa183fa28b0792",
"executor": "61aa20ee25ef06b69920a505",
"reviewer": "61aa11644afa183fa28b0792",
"status": "Invited",
"_id": "61aa213aeaf2af804aa1e591",
"createdAt": "2021-12-03T13:52:58.772Z",
"updatedAt": "2021-12-03T13:52:58.772Z"
},
...
]

我需要通过发出带有值的get请求来将中的一些属性转换为对象。类似的东西:

this.http.get<Application>(
`${environment.API_ENDPOINT}/applications/assigned?status=completed`
).pipe(
map(aplication => 
{
...aplication,
vacancy:this.http.get(`${environment.API_ENDPOINT}/vacancys/`+ aplication.vacancy),
executor:this.http.get(`${environment.API_ENDPOINT}/candidates/`+ aplication.executor)
}
)

您需要使用;"高阶映射算子";,其将在内部订阅可观测到的并发射其发射。

在您的情况下,switchMap将对此起作用。由于您需要进行两个不同的调用,我们可以使用forkJoin创建一个可观察的对象,该对象在收到两个结果时都会发出:

myObj$ = this.http.get<Application>('/applications/assigned').pipe(
switchMap(aplication => forkJoin({
vacancy  : this.http.get(`environment.API_ENDPOINT}/vacancys/${vacancy}`),
executor : this.http.get(`environment.API_ENDPOINT}/candidates/${executor}`)
}).pipe(
map(({vacancy, executor}) => ({...aplication, vacancy, executor})
))
);

最新更新