使用 flatMap 将数组转换为项目序列
Convert array to sequence of items with flatMap
在 RxJS 中,我想将某个时刻的数组转换为数组中的一系列项目。我找到了两种方法:选项 1 和 2,我猜,做同样的事情:
const obj = { array: [1, 2, 3, 4, 5] };
const observable = Observable.of(obj);
// Option 1
observable.flatMap(x => {
return Observable.from(x.array);
}).subscribe(console.log);
// Option 2
observable.flatMap(x => x.array).subscribe(console.log);
// Option 3 ?
是否有更好/更好的方式来表达我正在做的事情,我的意思是没有 flatMap
运算符?
我认为您已经到达了最短的路径。我可能建议的唯一改进是完全避免使用回调函数:
const obj = { array: [1, 2, 3, 4, 5] };
const observable = Observable.of(obj);
observable
.pluck('array')
.concatAll() // or mergeAll()
.subscribe(console.log);
在 RxJS 中,我想将某个时刻的数组转换为数组中的一系列项目。我找到了两种方法:选项 1 和 2,我猜,做同样的事情:
const obj = { array: [1, 2, 3, 4, 5] };
const observable = Observable.of(obj);
// Option 1
observable.flatMap(x => {
return Observable.from(x.array);
}).subscribe(console.log);
// Option 2
observable.flatMap(x => x.array).subscribe(console.log);
// Option 3 ?
是否有更好/更好的方式来表达我正在做的事情,我的意思是没有 flatMap
运算符?
我认为您已经到达了最短的路径。我可能建议的唯一改进是完全避免使用回调函数:
const obj = { array: [1, 2, 3, 4, 5] };
const observable = Observable.of(obj);
observable
.pluck('array')
.concatAll() // or mergeAll()
.subscribe(console.log);