I'm new to Angular and I'm filtering the user with the id same as in the JWT payload.
I can do this in the subscribe() of the Observable like this:
this.route.data.subscribe((data) => {
this.users = data.users.filter((u: User) => u.id !== +this.authService.decodedToken.nameid);
});
I also can do this in the pipe() method like:
this.route.data
.pipe(map(data => data.users.filter((u: User) => u.id !== +this.authService.decodedToken.nameid)))
.subscribe((data) => {
this.users = data;
});
Resolver code (probably irrelevant to the question):
resolve(route: ActivatedRouteSnapshot): Observable<User[]> {
return this.userService.getUsers().pipe(
catchError(error => {
this.alertify.error('Problem retrieving data');
this.router.navigate(['/home']);
return of(null);
})
);
}
Now why should I use the pipe() method rather than filtering directly in the subscribe() method are there any advantages or disadvantages of using pipe() in this scenario? Which of the two is the conventional Angular way of doing this?
I would think that the pipe() would be a little bit slower, since it first maps and then filters.