问题很简单:如何从Observable<Array<T>>
转换为Observable<T>
?
如果您能用rxJS或RxJava写答案,我会很感激,但其他任何语言都很好。
答案 0 :(得分:3)
根据您的预期结果,有多种方法可以解决您的问题。这是一个fiddle:
console.clear();
var x = Rx.Observable.of([1, 2, 3], [4, 5, 6]);
x.subscribe(item => {
console.log('Without flatMap: ' + JSON.stringify(item));
//Without flatMap: [1, 2, 3]
//Without flatMap: [4, 5, 6]
});
x.flatMap(item => {
return item;
}
).subscribe(item => {
console.log('With flatMap: ' + item);
//With flatMap: 1
//With flatMap: 2
//With flatMap: 3
//With flatMap: 4
//With flatMap: 5
//With flatMap: 6
});
x.reduce((a, b) => {
return a.concat(b);
}).subscribe(item => {
console.log('With reduce: ' + JSON.stringify(item));
//With reduce: [1, 2, 3, 4, 5, 6]
});
x.scan((a, b) => {
return a.concat(b);
}).subscribe(item => {
console.log('With scan: ' + JSON.stringify(item));
//With scan: [1, 2, 3]
//With scan: [1, 2, 3, 4, 5, 6]
});
答案 1 :(得分:1)
对于Java,Observable.from()
就是你想要的:
Observable<List<String>> listObservable = Observable.just(Arrays.asList("why", "hello", "there"));
listObservable.flatMap(Observable::from)
.subscribe(str -> System.out.print(str + "-"),
throwable -> {});