相关疑难解决方法(0)

在RxJS Observable中"展平"数组的最佳方法

我的后端经常将数据作为数组返回到RxJS 5 Observable中(我使用的是Angular 2).

我经常发现自己想要使用RxJS运算符单独处理数组项,我使用以下代码(JSBin)执行此操作:

const dataFromBackend = Rx.Observable.of([
  { name: 'item1', active: true },
  { name: 'item2', active: false },
  { name: 'item3', active: true }
]);

dataFromBackend
  // At this point, the obs emits a SINGLE array of items
  .do(items => console.log(items))
  // I flatten the array so that the obs emits each item INDIVIDUALLY
  .mergeMap(val => val)
  // At this point, the obs emits each item individually
  .do(item => …
Run Code Online (Sandbox Code Playgroud)

rxjs rxjs5 angular

34
推荐指数
3
解决办法
2万
查看次数

标签 统计

angular ×1

rxjs ×1

rxjs5 ×1