Filter first item in Observable

I have the following component:

export class UserComponent implements OnInit {

  user$: Observable<UserModel>;

  constructor(private userService: UserService) { }

  ngOnInit() {

    this.user$ = this.getUser();

  }

  getUser(): Observable<UserModel> {

    return this.userService.getByUserId(25).pipe(map((payload: Payload<UserResponse>) => {

      return payload.result.map((response: UserResponse) => { 

        return {
          id: response.id,
          name: response.name,
          // Other mapping properties
        };

      });

    }));

  }
}

With my code getUser() should return a Observable<UserModel[]>.

But I know that in this case payload has only one UserResponse.

So I would like getUser() method to return Observable<UserModel>.

How to adjust the pipe / map code to return an Observable of one UserModel?

Payload

Payload is:

export class Payload<T> {

  errors: Error[];
  paging: Paging;
  result: T[];

  constructor(result: T[], paging?: Paging, errors?: Error[]) {

    this.errors = errors;
    this.paging = paging;
    this.result = result;

  }

}

Source: Angular

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.