简体   繁体   中英

RXjs groupBy - Group AngularFire Firestore collection

I'm trying to group a AngularFire Firestore Collection (Observable[]) by a field, say 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'}
]

into

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

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

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

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

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM