Search code examples
arraysangulartypescriptrxjsobservable

Forkjoin an array of observables


I have an array of observables,

const resultObservable: Observable<searchObject[]> [];
let i : 0;
keywordsArray.foreach(keyword => {
    resultObservable[i] = getSearchResult(keyword); //function returns an array of observable searchObject
    i++;
});

now I have to concat all the resultObservable together, how can I achieve this?


Solution

  • You can use Array.map() to iterate over keywords and use concat() to combine them:

    const resultObservable: Observable<searchObject[]>[] = keywordsArray.map(keyword => 
        getSearchResult(keyword)
    );
    
    concat(resultObservable).subscribe(() => { /* Do Some Stuff */ })