`过滤器 rxjs 操作符 对结果值更改`

huangapple go评论78阅读模式
英文:

filter rxjs operator on result valuechanges

问题

我正在从 Angular (14.2) 查询一个 Firestore (7.5.0) 数据库。where 子句只允许一个范围/不等式谓词,所以我尝试使用 filter 来影响第二个。这段代码

let project$: Observable<Project[]> = this.firestore.collection<Project>('Projects',
ref => ref.where('EndDt', '>=', startDt)).
valueChanges().pipe(filter((proj) => { // 一次获取整个数组,所以要么全部要么都不要
console.log('proj: ', proj) ;
return proj['StartDt'] <= endDt ;
})) ;


一次性将所有结果(110个文档)发送给了 filter,因此我的真假返回看起来像是一切或无。我这样结构是否有误?谢谢,
英文:

I am querying a firestore (7.5.0) db from angular (14.2). The where clause(s) allow only one range/inequality predicate, so I am trying to use filter to affect the second. This code

let project$: Observable&lt;Project[]&gt; =  this.firestore.collection&lt;Project&gt;(&#39;Projects&#39;,
      ref =&gt; ref.where(&#39;EndDt&#39;, &#39;&gt;=&#39;, startDt)).
      valueChanges().pipe(filter((proj) =&gt; {    // Gets whole array once so all or nothing
        console.log(&#39;proj: &#39;, proj) ;
        return proj[&#39;StartDt&#39;] &lt;= endDt ;
      })) ;

Sends filter the entire result (110 documents) all at once so my true or false return seems like an all or nothing (I could iterate thru the array ... but again it's all or nothing). Am I structuring this wrong? Thanks,

答案1

得分: 2

RxJs的筛选操作符不会筛选响应,它会筛选流。如果一个值通过了筛选函数,那么流会发出该值,否则流不会发出。如果你想筛选结果,你需要使用RxJs的映射操作符来转换发出的值,并在发出的数组上使用筛选器。

obs$.pipe(map(results => results.filter(val => filterFunction(val))));
英文:

The RxJs filter operator does not filter the response, it filters the stream. If a value passes the filter function then the stream emits the value other wise the stream doesn't emit. If you want to filter the results you need to use the RxJs map operator to transform the emitted value and use the filter on the emitted array.

obs$.pipe(map(results =&gt; results.filter(val =&gt; filterFunction(val))));

huangapple
  • 本文由 发表于 2023年3月23日 10:03:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/75818710.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定