Rxjs get distinct values of property value in array

谁说胖子不能爱 提交于 2019-12-11 04:44:11

问题


I am trying to get distinct values of a property in an observable array.

  let pt$ = Observable.of([{planTypeID : 1, description : 'test 1'}, 
                                {planTypeID : 2, description : 'test 2'}]);
    let planTypeIDs$ = pt$
        .flatMap(a => a)
        .map(a => a.planTypeID).distinct().toArray();

Is this the right way to do it in rxjs, or is there a better way?


回答1:


  1. You could use .from instead of .of, that should spare you the .flatMap
  2. distinct will check the reference by default, so if you want to compare for contents you should create some kind of hash or make a custom comparer - but maybe that's not required here.

let pt$ = Observable.from([{planTypeID : 1, description : 'test 1'}, 
                            {planTypeID : 2, description : 'test 2'}]);
let planTypeIDs$ = pt$
    .map(a => a.planTypeID)
    .distinct()
    .toArray();


来源:https://stackoverflow.com/questions/41961982/rxjs-get-distinct-values-of-property-value-in-array

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!