Guardian
Guardian

Reputation: 399

Angular 6, Map http response to a Model

I am currently researching how I can map an http response to a defined model in angular.

In such a way that I can "push" the response into a class of my model, that only assigns the properties on the model and ignores all other.

Lets say that i have a user model that has

And the http response contains also

As an end result, I would like to receive a User class that has the Name & Email properties filled out and the UUID and Status are just ignored.

I can't seem to find a clean example nor explanation about this.

All help is much appreciated!

Upvotes: 2

Views: 8603

Answers (1)

JB Nizet
JB Nizet

Reputation: 692121

You basically want something like

getUser(): Observable<User> {
  return this.httpClient.get<any>(url).pipe(
    map(data => new User(data.name, data.email))
  );
}

Upvotes: 3

Related Questions