Angular 我如何通过订阅一个可观察对象来过滤ID

Angular 我如何通过订阅一个可观察对象来过滤ID,angular,observable,subscription,Angular,Observable,Subscription,下面我订阅了一个可观察的,但只是更新了第一个对象的值。我正在通过ID=413我想过滤ID,这样我只能得到那个对象,有人能帮我吗 谢谢 this.Ordersummary.subscribe( r => { if (r.length != 0) { this.thepackage = r[0]; this.emptySummary = true; } else {

下面我订阅了一个可观察的,但只是更新了第一个对象的值。我正在通过ID=413我想过滤ID,这样我只能得到那个对象,有人能帮我吗

谢谢

this.Ordersummary.subscribe(
        r => {
            if (r.length != 0) {
                this.thepackage = r[0];
                this.emptySummary = true;
            } else {
                this.router.navigate(['packages/gbr/public']);
            }
        });

您始终可以使用贴图和过滤器。或者,如果您希望针对特定值使用“仅订阅”触发器,则必须首先进行筛选

大概是这样的:

pipe(
  filter(item => item.ID === 413)
).subscribe(val => console.log(val));

如果您只想过滤必要的id,那么您可以创建一个简单的方法,如下所示

filterById(yourId: number) {
    this.filteredPackage = this.thepackage.filter(package => package.id === yourId);
}
说明:

将响应分配给subscribe()中的
this.thepackage
对象后,可以通过
this.thepackage
对象全局访问您的响应。要获得ID为413的单个记录,只需将ID传递到方法
filterById()
。由于修改原始对象不好,我们将新筛选的项分配给新变量this.filteredPackage

this.filterById(yourId);    // yourId is 413 here

当我单击项413时,控制台中没有任何内容

这里的ID是什么?
this.packageid=parseInt((this.route.snapshot.paramMap.get('ID'))谢谢,但现在我得到的属性“id”在类型“Ordersummary[]”上不存在
this.Ordersummary.pipe(筛选器(item=>item.id==this.packageid)).subscribe(r=>{if(r.length!=0){console.log('r:',r[0]);this.thepackage=r[0];this.emptySummary=true;}else{this.router.navigate(['packages/gbr/public']);})this.Ordersummary.pipe(
        filter((item: any) => item.id === this.packageid)
    ).subscribe(val => console.log('VAL: ', val));