Rxjs在数组中获取属性值的不同值 [英] Rxjs get distinct values of property value in array

查看:54
本文介绍了Rxjs在数组中获取属性值的不同值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在可观察的数组中获取属性的不同值.

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();

这是在rxjs中执行此操作的正确方法,还是有更好的方法?

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

推荐答案

  1. 您可以使用 .from 而不是 .of ,这样可以省去 .flatMap
  2. distinct 默认情况下会检查引用,因此,如果您想比较内容,则应该创建某种哈希或创建自定义比较器-但这也许不是必需的.
  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();

这篇关于Rxjs在数组中获取属性值的不同值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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