Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

358
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda