Rxjs - 如何提取数组中的多个值并将它们同步反馈给可观察流 [英] Rxjs - How can I extract multiple values inside an array and feed them back to the observable stream synchronously

查看:30
本文介绍了Rxjs - 如何提取数组中的多个值并将它们同步反馈给可观察流的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从事件流中创建了一个 Rx.Observable:

Rx.Observable.fromEvent(recognizeStream, 'data')

其中每个数据事件如下所示:

{ 错误:空,替代:[result1, result2, result3] }

我想提取 alternatives 数组中的每个值并将它们合并到流中.我需要查看哪些

concatMap

concatMap 将在

switchMap

但是当新的发射可用时,switchMap 将放弃前一个流,并切换从新流中发射值:

I have created a Rx.Observable from a stream of events:

Rx.Observable.fromEvent(recognizeStream, 'data')

In which every data event looks like this:

{ error: null, alternatives: [result1, result2, result3] }

I want to pluck every value inside the array of alternatives and merge those into the stream. What operators do I have to look at?

As far as I know the flatMap and concatMap could do the job but I don't get the idea from their example.

Can somebody explain which operator i should use and provide me with an example?

解决方案

The family of xxxMap() operators all deal with higher-order Observables. Which means they allow you to create Observables inside the main Observable and inline the resulting values into the primary stream. So you could read the type signature as Observable<Observable<T>> => Observable<T>

Given a stream which every emission x is an Observable containing 4 value emissions:

input:  --x----------x 
flatMap   a-a-a-a-|  b-b-b-b-|
result: --a-a-a-a----b-b-b-b-|

Typecasting xxxMap(myFnc) return values

The xxxMap() operators all work with results of type Observable, Promise or Array. Depending on what you put into it will get converted to Observable if needed.

Rx.Observable.of('')
  .flatMap(() => [1,2,3,4])
  .subscribe(val => console.log('array value: ' + val));

Rx.Observable.of('')
  .flatMap(() => Promise.resolve(1))
  .subscribe(val => console.log('promise value: ' + val));

Rx.Observable.of('')
  .flatMap(() => Promise.resolve([1,2,3,4]))
  .subscribe(val => console.log('promise array value: ' + val));

Rx.Observable.of('')
  .flatMap(() => Rx.Observable.from([1,2,3,4]))
  .subscribe(val => console.log('Observable value: ' + val));

<script src="https://cdnjs.cloudflare.com/ajax/libs/rxjs/5.0.3/Rx.js"></script>

In your case you can easily flatMap the objects and return the arrays:

Rx.Observable.of({ error: null, alternatives: ['result1', 'result2', 'result3'] })
  .flatMap(val => val.alternatives)
  .subscribe(console.log);

<script src="https://cdnjs.cloudflare.com/ajax/libs/rxjs/5.0.3/Rx.js"></script>

Difference between all xxxMap operators

What does mergeMap do

flatMap, better known as mergeMap will merge all emissions whenever they come into the main stream.

concatMap

concatMap will wait for all emissions to complete before concat the next stream:

switchMap

but switchMap will abandon the previous stream when a new emission is available and switch to emit values from the new stream:

这篇关于Rxjs - 如何提取数组中的多个值并将它们同步反馈给可观察流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆