简体   繁体   English

RXJS 组合可观察对象表

[英]RXJS combine a table of observables

i am getting observables from a response from Firestore.我从 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.在我想将这个 observables 列表与 RXJS 组合运算符结合起来之后。 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.因为我现在从来没有提前将结果的数量推送到我的数组中,所以我需要在组合运算符中动态获取 observables。

I tried the.map array to get the content of the array of observables out, but it's not working.我尝试了 .map 数组来获取 observables 数组的内容,但它不起作用。 My idea is to arrive to this result: const combineAll = merge(queriesTab[0], queriesTab[1], queriesTab[2]).pipe(mergeAll());我的想法是得出这个结果: const combineAll = merge(queriesTab[0], queriesTab[1], queriesTab[2]).pipe(mergeAll()); dynamically.动态的。

I tried const combineAll = merge(requetesTab.map(id => id+',')).pipe(mergeAll());我试过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.我试图 array.join() 创建一个可观察字符串的列表,但它不起作用。 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.我很确定.map 是可能的,但我现在不知道。 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?我在阅读这篇文章时找到了答案: 如何将 Observables 数组与 RxJS 6.x 和 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.真正理解 map、传播算子等新功能在 JavaScript 中的工作方式需要花费大量时间。 Especially when you come from the old school of the for and while iterators:)尤其是当您来自 for 和 while 迭代器的老派时:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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