Home > Enterprise >  Add find inside pipe
Add find inside pipe

Time:03-17

I have the following code :

await this.panelService.getActivePanels().subscribe(activePanels => {
      panel = activePanels.find(panel => panel.serviceId === serviceId);
    });

I want to add a pipe before the subscribe, that does the find operation, so when I subscribe I will get just the one panel returned by the find, is that possible? and how would it look?

CodePudding user response:

What you are looking for is called map and you can use it like this:

this.panelService.getActivePanels().pipe(
  map(activePanels => activePanels.find(panel => panel.serviceId === serviceId))
).subscribe(panel => {
  console.log(panel);
});

CodePudding user response:

import {map} from "rxjs/operators";

this.panelService.getActivePanels()
.pipe(map(activePanels => activePanels.find(panel => panel.serviceId === serviceId)))
.subscribe(foundPanel => panel = foundPanel);
  • Related