例:
var s1 = Observable.of([1, 2, 3]);
var s2 = Observable.of([4, 5, 6]);
s1.merge(s2).subscribe(val => {
console.log(val);
})
Run Code Online (Sandbox Code Playgroud)
我想得到[1,2,3,4,5,6]
代替
[1,2,3]
[4,5,6]
max*_*992 20
forkJoin 工作井,你只需要压扁阵列数组:
const { Observable } = Rx;
const s1$ = Observable.of([1, 2, 3]);
const s2$ = Observable.of([4, 5, 6]);
Observable
.forkJoin(s1$, s2$)
.map(([s1, s2]) => [...s1, ...s2])
.do(console.log)
.subscribe();
Run Code Online (Sandbox Code Playgroud)
输出: [1, 2, 3, 4, 5, 6]
Plunkr演示:https://plnkr.co/edit/zah5XgErUmFAlMZZEu0k?p = preview
我的看法是使用Array.prototype.concat()进行zip和映射:
https://stackblitz.com/edit/rxjs-pkt9wv?embed=1&file=index.ts
import { zip, of } from 'rxjs';
import { map } from 'rxjs/operators';
const s1$ = of([1, 2, 3]);
const s2$ = of([4, 5, 6]);
const s3$ = of([7, 8, 9]);
...
zip(s1$, s2$, s3$, ...)
.pipe(
map(res => [].concat(...res)),
map(res => res.sort())
)
.subscribe(res => console.log(res));
Run Code Online (Sandbox Code Playgroud)