Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

212
Views
How to convert an Observable stream into an Observable Array

I am trying to retrieve a list of dogs from a database. I want to retrieve the list of dogs as an Observable<Dog[]>. However when I call toArray() or use any other method to try convert the incoming stream to an array I either receive no data when calling the retrieveDogs(dogsId) method. How does one go about retrieving an Observable array instead of just an Observable stream?

    retrieveDogs(dogIds : Array<string>): Observable<Dog[]>{
        return Observable.from(dogIds)
                .map(dogId => this.retrieveDog(dogId)) 
                .flatMap(dogObservable => dogObservable)
                .toArray();
     }

    retrieveDog(dogId : string) : Observable<DogEntity> {
     //afDB -> AngularFireDatabase that returns an observable with the data from the firebase database
        return this.afDB.object(DB_DOGS + DB_DASH + dogId)
                .map(dogObject =>DogEntity.convertObject(dogId,dogObject)
    }
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

You could do that to retrieve your data:

retrieveDogs(dogIds : Array<string>): Observable<Dog[]>{
  let obs = dogIds.map(dogId => this.retrieveDog(dogId));

  return Observable.forkJoin(obs);
}

retrieveDog(dogId : string) : Observable<DogEntity> {
  return this.afDB.object(DB_DOGS + DB_DASH + dogId)
    .map(dogObject => DogEntity.convertObject(dogId,dogObject));
}

Simple demo here http://jsbin.com/sotunopocu/2/edit?js,console

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!