0

I need some assistance trying to loop through array of string numbers and assign http get requests to each string number and then assign the results to a single reactive observable. I was able to pull this off at the component level but I need the result in a observable that comes from my service. I am trying to leverage RxJs but having issues figuring out the correct syntax here. Hopefully this makes sense, thanks.

myservice

constructor(http: HttpClient)

 issueNumberString: string[];
 issuesObservable$;
 newIssueObservable$;
 private URL = '/my/api/stuff/';


getInfo() {
 for (let index = 0; index < this.issueNumbersString.length; index++) {
this.https.get<any[]>(this.URL + this.issueNumbersString[index])
.subscribe((response: any) => {
 return this.issuesObservable$.push(...response.records)  
})
}

this.newObservable$ = this.getInfo();
}

.ts

newObservable$ = this.myservice.newObservable$;

constructor(private service: myservice)

html

<div *ngIf="newObservable$ | async as entry">
    <div *ngFor="let n of entry">
1

1 Answer 1

4

goal is alittle unclear, but you should use forkJoin to run an array of observables:

getInfo() {
  return forkJoin(
    this.issueNumbersString.map(i => 
      this.http.get<any[]>(this.URL + i))
  ).pipe(
    map(recordsArrays => recordsArrays.reduce((arr, r) => arr.concat(r.records), [])
  )
}

the forkJoin gives you an array of your responses, then the map operator at the end to reduces it to one array of all records.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.