如何使用RXJS运算符基于重复值对Firebase可观察列表进行排序 [英] How to sort Firebase observable list based on the duplicated value using RXJS operators

查看:115
本文介绍了如何使用RXJS运算符基于重复值对Firebase可观察列表进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用rxjs运算符来处理firebase可观察列表。
我需要根据重复的值(某个id)对不同的列表进行排序。



这是我的代码:

  this.places $ 
.flatMap((x)=> {
console.log(x)
return x;
})
.distinct((places:any)=> {
console.log(places.googleId)
return places.googleId;
})
.subscribe(snap => {
this.tempArray.push(snap);
})

这是(places.googleId)的日志





因此我需要根据最重复的数字对此ID或列表进行排序,例如

  1-Eg9MZWJhbm9uLCBCZWlydXQ 
2-EhZIYW1yYSwgQmVpcnV0LCBMZWJhbm9u
3-ChIJR9lei8pAHxuremilQojBkYc

请帮忙,



谢谢

解决方案

我将采用的基本方法是使用计数器排序来减少数组。我假设您只想在结尾处使用一个值来表示整个流的排序结果。所以我添加了 last 运算符来等待完成。



如果你想用RxJs这样做,你可以使用 scan 运算符来减少流,然后在 map 运算符中对其进行排序。



  function countDistinct(map,current){let entry = map.get(current.id); if(!entry){entry = {count:0,d​​ata:current}; map.set(current.id,entry); } entry.count ++; return map;} function sortCountDescending(a,b){return a.count< b.count? 1:a.count> b.count? -1:0;} Rx.Observable.of([{id:1},{id:2},{id:3},{id:2},{id:2},{id:3}]) .flatMap(x => x).scan(countDistinct,new Map())。last()//如果你想要一个渐进的result.map(x => Array.from(x.values()),请删除它.sort(sortCountDescending).map(x => x.data))。subscribe(x => {console.log(x);});  

< pre class =snippet-code-html lang-html prettyprint-override> < script src =https://cdnjs.cloudflare.com/ajax/libs/rxjs/5.5.6/Rx .js>< / script>



编辑:你可以使用 reduce 运算符代替 .scan(...)。last()。它基本上做同样的事情。


I'm using the rxjs operators to handle the firebase observable lists. I need to sort distinct list based on the duplicated value (certain id).

this is my code:

  this.places$
  .flatMap((x)=>{
    console.log(x)
    return x;
  })  
 .distinct((places:any)=>{ 
   console.log(places.googleId)
     return places.googleId;
  })
  .subscribe(snap=>{
    this.tempArray.push(snap);
  }) 

this is the log of (places.googleId)

so i need to sort this ids or the list according of the most duplicated number to be for example

    1-Eg9MZWJhbm9uLCBCZWlydXQ 
    2-EhZIYW1yYSwgQmVpcnV0LCBMZWJhbm9u
    3-ChIJR9lei8pAHxUREmilQojBkYc

Any help please,

thanks

解决方案

The basic approach that I would take is to reduce the array using a counter sort. I'm assuming that you want just one value at the end that represents the sorted result of the whole stream. So I added the last operator to wait for completion.

If you wanted to do this with RxJs then you could use the scan operator to reduce the stream and then sort it in a map operator.

function countDistinct(map, current) {
  let entry = map.get(current.id);
  if (!entry) {
    entry = {
      count: 0,
      data: current
    };
    map.set(current.id, entry);
  }
  entry.count++;
  return map;
}

function sortCountDescending(a, b) {
    return a.count < b.count ? 1
      : a.count > b.count ? -1
      : 0;
}

Rx.Observable.of([
  { id: 1},
  { id: 2},
  { id: 3},
  { id: 2},
  { id: 2},
  { id: 3}
])
.flatMap(x => x)
.scan(countDistinct, new Map())
.last() // remove this if you want a progressive result
.map(x => Array.from(x.values()).sort(sortCountDescending).map(x => x.data))
.subscribe(x => { console.log(x); });

<script src="https://cdnjs.cloudflare.com/ajax/libs/rxjs/5.5.6/Rx.js"></script>

Edit: You can use the reduce operator instead of .scan(...).last(). It basically does the same thing.

这篇关于如何使用RXJS运算符基于重复值对Firebase可观察列表进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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