2017-08-01 27 views
-1

這是怎麼了現在的工作:角2 Observable.forkJoin與循環

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 ...

我正在嘗試編寫一個函數,它應該將params作爲一個ID數組和一個URL數組。 With Observable.forkJoin for循環應執行每個請求getById到Array中的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()); 

    } 

) 
} 

我該怎麼做?

+1

的可能的複製[Observable.forkJoin和數組參數](https://stackoverflow.com/questions/35676451/observable-forkjoin-and-array-argument) –

回答

0

嘗試使用此

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