简体   繁体   English

如何映射两个 rxjs 流?

[英]How to map two rxjs streams?

I have two async streams:我有两个异步流:

users = new Subject<User[]>();
groups$ = new Subject<Rights>();
mapped$ = new Subject();

I want to map first stream with value from the second.我想用第二个流的值映射第一个流。 Using imperative way I do this like:使用命令式方式我这样做:

users.foreach((user: User => {
    user.value = groups.find((g) => g.user == user.id)?.value
});
 

How to do that using RXJS to get mapped result in mapped$ ?如何做到这一点使用RXJS得到的映射结果mapped$

You can use combineLatest to emit when either users$ or groups$ emits, then map to add the group to each user:您可以使用combineLatestusers$groups$发出时发出,然后map以将组添加到每个用户:

combineLatest([users$, groups$]).pipe(
  map(([users, groups]) => users.map(user => {
    user.value = groups.find(g => g.user == user.id)?.value
    return user
  }))
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 NGRX / RXJS:如何将可观察流递归映射为单个平面流 - NGRX / RXJS: How to recursively map observable streams into a single flat stream 有条件地将两个流与RxJ合并 - Conditionally merge two streams with RxJs 如何使用带有HTTP订阅和rxjs映射来获取两个对象数组 - How to use http subscribe with rxjs map to fetch two arrays of objects 如何合并两个数据流,根据对象字段对其进行过滤,然后将它们合并到 RxJS 中? - How do I combine two data streams, filter them based on an objects field, and then merge them in RxJS? Rxjs:将两个流与一个依赖于另一个的流合并 - Rxjs: Merge two streams with one stream that depends of another 使用RxJS .flatmap在Angular2中组合两个可观察到的流 - Combining two observable streams in Angular2 using RxJS .flatmap 组合两个rxjs流并仅在第一个流发生时才发射 - Combine two rxjs streams and emit only if the first one it's true 如何在 GET 请求中将两个主题/操作流作为 HTTP 参数传递,以使用 RxJS 返回 Angular 中的单个对象/对象数组? - How to pass two Subjects/Action streams as HTTP Params in a GET request to return a single object/Array of objects in Angular using RxJS? rxjs如何传递可观察到的地图 - rxjs how to pass observable to map 使用 map 组合两个 rxjs 可观察对象的数据 - Combing data of two rxjs observables using map
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM