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

how convert response object property from array in to request rxjs

API returns 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"
},
...
]

而且我需要通过使用值进行获取请求将一些属性转换为对象。 类似的东西:

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)
}
    )

您需要使用“Higher Order Mapping Operator”,它将在内部订阅一个 observable 并发出它的发射。

在您的情况下,switchMap will work for this. Since you need to make two different calls, we can use 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})
    ))
);