如何为数组数组实现rxjs zip运算符?

时间:2016-04-01 02:03:37

标签: javascript rxjs

我有一个提供数组数组的源代码。所有数组都具有相同数量的元素。 如何使用rxjs zip运算符转换源数组,如下所述,以便我可以轻松地进行映射?提前谢谢!

var source = Rx.Observable.from([[1, 2, 3], ["a", "b", "c"], ["do", "re", "mi"]]);
// I would like to transform to:
// [[1, "a", "do"], [2, "b", "re"], [3, "c", "mi"]]

1 个答案:

答案 0 :(得分:0)

您可以将每个数组转换为Observable,然后压缩:

var myArrays = [
  [1, 2, 3],
  ['a', 'b', 'c'],
  ['do', 're', 'mi']
];

var myObservables = myArrays.map(xs => Rx.Observable.from(xs));
var zipped$ = Rx.Observable.zip(myObservables);

zipped$.subscribe(x => console.log(x));

或者,您可以放弃从ArrayObservable的转换,并使用lodash的zip来组合数组:

var zipped = _.zip(...myArrays)