Reputation: 109
i am currently struggeling by filtering a Map Observable with RxJS. What i try to do is to do reduce my Map-Object by a filter function before subscribing to it.
I init the Subject like this:
public myObjects: ReplaySubject<Map<number, MyClass>> = new ReplaySubject<Map<number, MyClass>>(1);
And that's how I try to filter:
public subscribeToIncomingObjects() {
this.myObjects.map( (currentValue:Map<number, MyObject>) => Array.from(currentValue.values())).filter(
(con:myObject, index) => {
return someCondition;
}
);
}
For some reason con is of type Array and not a single instance. I would really appreciate any advise.
Upvotes: 2
Views: 763
Reputation: 21564
First things first,
You are dealing with an Observable
:
this.myObjects.map( (currentValue:Map<number, MyObject>) => Array.from(currentValue.values()))
returns an Observable<MyObject[]>
. So if you call .filter()
on that Observable
, you are filtering Observable's broadcast.
You could just call your filter like this :
public subscribeToIncomingObjects() {
this.myObjects.map((currentValue: Map < number, MyObject > ) => {
return Array.from(currentValue.values())
.filter((con: myObject, index) => someCondition);
});
}
BUT you would still get an array, if you need a single instance you'd better use Array.prototype.find()
if it is available :
public subscribeToIncomingObjects() {
this.myObjects.map((currentValue: Map < number, MyObject > ) => {
return Array.from(currentValue.values())
.find((con: myObject, index) => someCondition);
});
}
or, still using Array.prototype.filter()
:
public subscribeToIncomingObjects() {
this.myObjects.map((currentValue: Map < number, MyObject > ) => {
return Array.from(currentValue.values())
.filter((con: myObject, index) => someCondition)[0];
});
}
Upvotes: 1