使用Rxjs过滤可观察到的

编程入门 行业动态 更新时间:2024-10-22 14:41:29
本文介绍了使用Rxjs过滤可观察到的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试获取当前用户的活动作业列表:

I'm trying to get a list of active jobs for the current user:

jobListRef$: Observable<Job[]>; ... this.afAuth.authState.take(1).subscribe(data => { if (data && data.uid) { this.jobListRef$ = this.database.list<Job>('job-list', query => { return query.orderByChild("state").equalTo("active"); }) .snapshotChanges().map(jobs => { return jobs.map(job => { const $key = job.payload.key; const data = { $key, ...job.payload.val() }; return data as Job; }); }) .filter(val => val.map(job => { return (job.employer == data.uid || job.employee == data.uid); }) ); } });

问题仅从过滤开始.根据官方文档,其参数的正确部分应返回布尔值,就像我的情况一样.但是仍然返回我整个条目而不过滤它们.

The problem begins only at the filtering. According to the official documentation the right part of its argument should return boolean, so like in my case. But still it returns me whole entries without filtering them.

推荐答案

我相信您想反转地图和过滤器运算符.

I believe you want to reverse the map and filter operators.

.map((jobs: Job[]) => jobs.filter((job: Job) => job.employer === data.uid || job.employee === data.uid ) );

(map将一个数组转换为另一个数组,filter缩小数组).

(map to transform one array into another, filter to reduce the array).

或者您可以将过滤器链接到执行类型转换的地图上,

Or you can chain filter on to the map that performs the type-conversion,

.map(jobs => { return jobs.map(job => { const $key = job.payload.key; const data = { $key, ...job.payload.val() }; return data as Job; }) .filter(job => job.employer === data.uid || job.employee === data.uid ) })

更多推荐

使用Rxjs过滤可观察到的

本文发布于:2023-10-27 18:47:27,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1534228.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:观察到   Rxjs

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!