本文介绍了RxJs Observable的可观察Zip数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个使用zip来组合固定数量的可观察量的示例,如下所示:

There is an example of using zip to combine a fixed number of observables like the following:

var range = Rx.Observable.range(0, 5);
var source = Observable.zip(
      range,
      range.skip(1),
      range.skip(2),
      function (s1, s2, s3) {
            return s1 + ':' + s2 + ':' + s3;
      }
)

我的问题是,如果我有一组可观察量(长度可以是任何东西),如何完成像拉链一样?

My questions is what if I have an array of observables (length can be anything), how to accomplish the same like zip?

var arr = [observable1, observable2, ..]; //like 100 of them!
var source = Observable.zip(
      //how to put each observable here?
      function (__how to put argument here?_) {
            return /* loop through arguments*/;
      }
)


推荐答案

理论上,您可以使用 .apply()参数变量的组合来完成此任务。完全披露:我没有尝试过这段代码。

In theory, you can use a combination of .apply() and the arguments variable to accomplish this. Full disclosure: I have not tried this code.

var arr = [observable1, observable2, ..]; //like 100 of them!
var source = Observable.zip.apply(Observable,
     arr.concat(function (varargs) { // using "varargs" purely for documentation
         Array.prototype.forEach.apply(arguments, function (observableValue) {
            /* do something with observableValue */;
         });
     })
);

这篇关于RxJs Observable的可观察Zip数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 16:12