Home > Mobile >  Retrieve an observable with an array of data
Retrieve an observable with an array of data

Time:04-17

Imagine, I have an array of observables, inside this array of observables I have other arrays of ReferenceData, I want to be able to retrieve an Observable with an Array of ReferenceData arrays, I'm a bit stuck on it for a couple of days, is there any other way to do that?

export class ReferenceData {
  id: number;
  caption: string;

  constructor(_id: number, _caption: string) {
    this.id = _id
    this.caption = _caption
  }
}

const observables = [
 of([new ReferenceData(1, 'Test'), new ReferenceData(2, 'Test 2')]),
];

const getData = (): Observable<[ReferenceData[]]> {
 return observables.map((obs) => {
  return obs.pipe(
    map((data) => {
      return [...data]
    })
  )
 })
}

Return expected from getData function: Observable<[ReferenceData[]]>

CodePudding user response:

You can use forkJoin operator like below :-

forkJoin(observables).subscribe((res: ReferenceData[][]) => {
   // your logic here
})

CodePudding user response:

const getData = () => from(obs).pipe(switchMap(ob => ob), toArray()).subscribe(console.log)
  • Related