Angular 2 Observable.forkJoin with for loop



这就是我现在的工作方式:

getTwoObjectById(url1: string, id1: string, url2: string, id2): any{
return Observable.forkJoin(
this.http.get(url1 + `/${id1}`, this.jsonWebToken()).map(res => 
res.json()),
this.http.get(url2 + `/${id2}`, this.jsonWebToken()).map(res => 
res.json())
);
}

我想用id1,url1,id2,url2,id3,url3来防止这种函数...

我正在尝试编写一个函数,该函数应将 ID 数组和 URL 数组作为参数。有了Observable.forkJoinfor 循环应该执行每个请求 getById 到数组中的后端 URL。 我的问题是通过 for 循环

getObjectsById(ids: Array<string>, urls: Array<string>): Observable<any>{
return Observable.forkJoin(
for(let i = 0; i++; i<ids.length) {
this.http.get(urls[i] + `/${ids[i]}`, this.jsonWebToken()).map(res => res.json());
}
)
}  

我该怎么做?

尝试使用这个

inputObject = [1, 2, 3, 4];

getObjectsById() {
let observableBatch = [];
this.inputObject.forEach((key) => {
observableBatch.push(this.http.get(url+key).map((res) => res.json()));
});
return Observable.forkJoin(observableBatch);
}

最新更新