Search code examples
angularobservablesubscription

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. 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?

Thanks

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

Solution

  • You can always use map and filter. 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));