gpt4 book ai didi

Angular 2 Observable.forkJoin 与 for 循环

转载 作者:行者123 更新时间:2023-12-02 14:38:06 26 4
gpt4 key购买 nike

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

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.forkJoin for 循环应该执行数组中的每个 getById 到 Backend-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);
}

关于Angular 2 Observable.forkJoin 与 for 循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45438959/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com