[英]rxjs “find” with observable of hashmap of objects
Say I have class hiObject
where it has variables id: number
and a value: number
inside.假设我有 class hiObject
其中有变量id: number
和value: number
里面。 \ \
Then I have hiObjects$: Observable<HashMap<hiObject>> =...
in an other class.然后我在另一个 class 中有hiObjects$: Observable<HashMap<hiObject>> =...
I wish to find the hiObject
inside this hashmap observable where id = 2
, and assign their value
to an digit$: Observable<number>
我希望在这个 hashmap observable 中找到hiObject
,其中id = 2
,并将它们的value
分配给digit$: Observable<number>
Currently I'm using this.digit$ = hiObjects$.pipe(map(hiObjects) => for(const index in hiObjects)...)
, basically looping through the entire hashmap and looking for when id
is equal to 2.目前我正在使用this.digit$ = hiObjects$.pipe(map(hiObjects) => for(const index in hiObjects)...)
,基本上循环遍历整个 hashmap 并寻找id
何时等于 2。
But I think there should be another alternate where you use rxjs find
and map
, wondering if anyone could help.但是我认为应该有另一个替代方案,您可以使用 rxjs find
和map
,想知道是否有人可以提供帮助。 Thank you!谢谢!
EDIT : Think hashmap does not allow find, so this question is useless now..编辑:认为 hashmap 不允许查找,所以这个问题现在没用了..
TS: TS:
export interface hiObject {
id?: number;
value?: number;
}
export class smth implements OnInit{
public digit$: Observable<number>;
public hiObjects$: Observable<HashMap<hiObject>> = ...
ngOnInit() {
this.digit$ = this.hiObjects$.pipe(
map((collections) => {
for (const index in collections) ...})
);
}
}
You're close, use .find
and then the .value
if it's actually found你很接近,使用.find
,然后使用.value
如果它真的找到了
this.digit$ = hiObjects$.pipe(
// this finds the hiObject that has an id of 2, and then returns the value
map(hiObjects => hiObjects.find(id => id === 2)?.value)
);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.