BadPiggie
BadPiggie

Reputation: 6379

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);
               })
            );

}

Upvotes: 0

Views: 169

Answers (1)

Fan Cheung
Fan Cheung

Reputation: 11380

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

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

         

          

Upvotes: 1

Related Questions