繁体   English   中英

rxjs “找到” 可观察到的对象的 hashmap

[英]rxjs “find” with observable of hashmap of objects

假设我有 class hiObject其中有变量id: numbervalue: number里面。 \

然后我在另一个 class 中有hiObjects$: Observable<HashMap<hiObject>> =...

我希望在这个 hashmap observable 中找到hiObject ,其中id = 2 ,并将它们的value分配给digit$: Observable<number>

目前我正在使用this.digit$ = hiObjects$.pipe(map(hiObjects) => for(const index in hiObjects)...) ,基本上循环遍历整个 hashmap 并寻找id何时等于 2。

但是我认为应该有另一个替代方案,您可以使用 rxjs findmap ,想知道是否有人可以提供帮助。 谢谢!

编辑:认为 hashmap 不允许查找,所以这个问题现在没用了..

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) ...})
    );
  }
}

你很接近,使用.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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM