14

I have an array of observables and want to pass to Rx.Observable.zip. I tried and it does not get subscribed at all.

Code snippet(just am example):

const sourceOne = Rx.Observable.of('Hello');
const sourceTwo = Rx.Observable.of('World!');
const sourceThree = Rx.Observable.of('Goodbye');
const sourceFour = Rx.Observable.of('World!');
const arr$ = [sourceOne, sourceTwo, sourceThree, sourceFour];

const zip$ = (a$) => Rx.Observable.zip(a$);

const subscribe = zip$(arr$).subscribe(val => console.log(val));

Is there a way to pass an array to Rx.Observable.zip?

softshipper
  • 32,463
  • 51
  • 192
  • 400
  • Would also be nice to know WHY this happens??? Why when THERE IS an overload that takes an array, it simply does nada! Very confusing and very time consuming to figure out... – rubmz Oct 13 '22 at 15:31

1 Answers1

32

Operator zip accepts only an unpacked array.

zip(sourceOne, sourceTwo, sourceThree, ...);

If you're using ES6 you can also use destructuring assignment with ...:

const zip$ = (a$) => zip(...arr$);

See live demo: https://jsbin.com/tinaxeq/1/edit?js,console

martin
  • 93,354
  • 25
  • 191
  • 226
  • First of all, thanks for your answer. No am I using javascript, is there anyway to do that too? – softshipper Jan 13 '17 at 09:26
  • 1
    I checked the [ES6 spec](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment) and you can use `Rx.Observable.zip(...arr$);` as well. If you're using ES5 then use `Rx.Observable.zip(sourceOne, sourceTwo, sourceThree);` – martin Jan 13 '17 at 09:33
  • 1
    If you are using ES5, you can use: `Rx.Observable.zip.apply(null, arr$);` – cartant Jan 13 '17 at 10:17
  • Just to note, ES6 is javascript. It's just a later version than you may be using e.g. ES5. – SoEzPz Jul 23 '19 at 04:47