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

354
Views
Flatten array of observables return by switchMap() in pipe()

I have a method getFolders() which returns a Observable.

function getFolders() {

  const data = [
       {
          name: "Photos",
          files: of(2) // observable
       },
       {
          name: "Document",
          files: of(5) // observable
       },
       {
          name: "Videos",
          files: of(2) // observable
       }
  ];

  return of(data);

}

I need to get the total files count with is 9 (2+5+2). I wrote a function to do the job by the following usage,

getFileCount.subscribe( totalCount => console.log(totalCount) );

// expects to log 9

The implementation is,

function getFileCount() {

   return getFolders()
            .pipe(
               switchMap( folderData => folderData.map( data => data.files ) ),
               // Above switchMap returns [ Observable, Observable, Observable ]
               // Now I need to complete all Observable and get flatten result like [2,5,2]

               mergeAll(),
               // I though the mergeAll will resolve all Observables and return the value
               
               switchMap( countArray => {

                   // expecting all values in a single array [2,5,2]
                   // but it executes for each value like
                   // 2
                   // 5
                   // 2

                   console.log(countArray);

                   return countArray.reduce(function(a, b) { return a + b; }, 0);
               })
            );

}
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Use forkJoin to wrap the observable array and remove mergeAll(), it'll execute each observable inside

switchMap( folderData => forkJoin(folderData.map( data => data.files ) )),

         

          
about 4 years ago · Juan Pablo Isaza 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!