问题
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:
- You could use
.frominstead of.of, that should spare you the.flatMap - 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