RXjs groupBy-组AngularFire Firestore集合 [英] RXjs groupBy - Group AngularFire Firestore collection

查看:96
本文介绍了RXjs groupBy-组AngularFire Firestore集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试按 userId 这样的字段对 AngularFire Firestore集合(Observable [])进行分组.

I'm trying to group a AngularFire Firestore Collection (Observable[]) by a field, say userId.

我有一组项目,每个项目都有不同的userId值,我需要将它们分组为多维数组.因此,进行转换.

I have a collection of items, each with varying userId values, and I need to group them into a multidimensional array. So, convert..

[ 
  {userID:1, color:'blue' }, 
  {userId:2, color:'green'}, 
  {userId:1, color:'orange'}
]

进入

[ 
  [
   {userID:1, color:'blue' }, 
   {userId:1, color:'orange'}
  ],
  [
   {userId:2, color:'green'}
  ]
]

我对RxJ完全陌生,我了解制作 Observable.of(itemsArray) groupBy 运算符的工作方式,但是我看不到如何使它与从 AgularFire

I'm totally new to RxJs, I get how the groupBy operator works when making an Observable.of(itemsArray), but I can't see how to get this to work with the Observable returned from AgularFire

let items:Observable[] = this.afs.collection<Item>('items')
  .snapshotChanges()
  .groupBy( item => item.userId ); //item is actually the array

看起来 AngularFire 返回了一个 Observable ,它发出了整个数组,而不是每个元素.

It looks like AngularFire returns an Observable that emits the Array as a whole, not each element.

如果我 map 它,那么我不就只有数组中的每个项目了,并且不能使用 groupBy 吗?

If I map it, dont I then just have each item in the array, and cant use the groupBy?

是否有关于如何按上述方式返回分组组数组的 Observable 数组的指针?

Any pointers on how to return a Observable array of grouped arrays as per above?

推荐答案

您可以使用以下方法简单地将数组的可观察值转换为数组值的可观察值:

You can simply turn your observable of array into an observable of the values of the array with:

myObservable.flatMap(arr => Rx.Observable.from(arr));

这篇关于RXjs groupBy-组AngularFire Firestore集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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