简体   繁体   中英

RXJS combine a table of observables

i am getting observables from a response from Firestore. Then i create a list of observables in an Array. After i want to combine this list of observables with the RXJS combine operator. Because i never now in advance the number of results to push in my array, i need to get the observables dynamically in the combine operator.

I tried the.map array to get the content of the array of observables out, but it's not working. My idea is to arrive to this result: const combineAll = merge(queriesTab[0], queriesTab[1], queriesTab[2]).pipe(mergeAll()); dynamically.

I tried const combineAll = merge(requetesTab.map(id => id+',')).pipe(mergeAll()); but its not working.

I tried to array.join() to create a list of observable strings, but it's not working to. I listed all the answers on the same subject without finding a response. I am pretty sure that it is possible with.map, but i don't now how. Thanks for your help.

var counter = 0;
      var queriesTab = [];
      for (counter=0; counter < secteurAct.length; counter++ ){
        var recup = this['requete' + counter];
        recup = this.firestore.collection('annonce', ref => ref
        .where("secteurActivite","==", secteurAct[counter])
        .orderBy("date"))
        .snapshotChanges().pipe(
          map(actions => actions.map(a => {
            const data = a.payload.doc.data();
            const id = a.payload.doc.id;
            return { id, ...data };
          }))
        );
        queriesTab.push(recup);
      }//End for

      const combineAll = merge(queriesTab[0], queriesTab[1], queriesTab[2]).pipe(mergeAll());
      return combineAll;

I found an answer reading this post: How do I merge an array of Observables with RxJS 6.x and Node.js?

const combineAll = merge(...queriesTab).pipe(mergeAll());

Using the spread operator it's working fine.

It takes a lot of time to really understand how the new functions as map, spread operators etc.. worked in JavaScript. Especially when you come from the old school of the for and while iterators:)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM