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

357
Views
Aplane la matriz de retorno de observables por switchMap() en pipe()

Tengo un método getFolders() que devuelve un 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); }

Necesito obtener el recuento total de archivos con 9 ( 2+5+2 ). Escribí una función para hacer el trabajo con el siguiente uso,

 getFileCount.subscribe( totalCount => console.log(totalCount) ); // expects to log 9

La implementación es,

 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 para envolver la matriz observable y eliminar mergeAll() , ejecutará cada observable dentro

 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!