简体   繁体   English

如何通过订阅 Observable 来过滤 ID

[英]How can I filter an ID by subscribing to an Observable

Below I am subscribing to an observable but just updating values for the first object.下面我订阅了一个可观察的但只是更新第一个 object 的值。 I am passing through ID = 413 I would like to filter the ID so I can only get that object, can anyone help me please?我正在通过 ID = 413 我想过滤 ID 所以我只能得到 object,有人可以帮我吗?

Thanks谢谢

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

You can always use map and filter.您始终可以使用 map 和过滤器。 Or if you want the subscribe to only trigger for a specific value, then you have to filter first.或者,如果您希望订阅仅触发特定值,则必须先进行过滤。

Something like this:像这样的东西:

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

If you want to filter only the necassary id then you may create a simple method like this,如果您只想过滤 necassary id,那么您可以创建一个像这样的简单方法,

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

Explanation:解释:

After assigning your response to the this.thepackage object inside subscribe(), your response is globally accessible via this.thepackage object.将您的响应分配给 subscribe() 内的this.thepackage object 后,您的响应可通过this.thepackage object 全局访问。 In order to get the single record who's ID is 413, just pass the ID into the method filterById() .为了获取 ID 为 413 的单条记录,只需将 ID 传递给方法filterById() Since it's bad to modify the original object, we assign the newly filtered item to a new variable this.filteredPackage .由于修改原始 object 是不好的,我们将新过滤的项目分配给一个新变量this.filteredPackage

this.filterById(yourId);    // yourId is 413 here
this.Ordersummary.pipe(
        filter((item: any) => item.id === this.packageid)
    ).subscribe(val => console.log('VAL: ', val));

I am not getting anything in console when I click on item 413当我单击项目 413 时,我在控制台中没有得到任何东西

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何过滤可观察数组? - How can I filter an observable array? 如何过滤Observable onKeyUp? - How can I filter an Observable onKeyUp? 我可以使用rxjs根据id的字符串数组过滤Observable - Can I use rxjs to filter an Observable against a string array of id's 根据 id 过滤 observable - Filter an observable depending on id Angular:如何在模板中订阅之前观察到 pipe - Angular: How to pipe Observable before subscribing in template 订阅Observable时如何获得最后一个值? - How to get last value when subscribing to an Observable? 为什么在订阅我的选择器 observable 时看不到来自 NGRX 的初始值? - Why can't I see the initialvalues from NGRX when subscribing to my selector observable? 从路由到非路由组件的通讯时,我们如何停止和继续订阅Observable方法 - How can we stop and resume subscribing to an Observable method while communicating from a routed to a non-routed component 为什么订阅返回 FireStore 文档快照列表的 Observable 我无法构建正确的对象列表? 我获得未定义的对象 - Why subscribing an Observable returning a list of FireStore document snapshot I can't build a proper list of objects? I obtain undefined objects 我如何获得状态的最后一个值? 无需订阅(rxJs) - How i can get last value of state? without subscribing (rxJs)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM