Angular 7: Give biding on an object that will receive a json from an HTTP GET

0

Let's say I have the typed object:

x: Endereco

public interface Endereco {
  rua: string;
  numero: number;
}

And I have a GET http that will feed this variable x. The problem is that in the request I get a json with the "neighborhood" parameter that I do not want to go to my x variable. How do I 'filter' this?

{
  rua: "nome da rua",
  numero: 34,
  bairro: "Não quero isso"
}

I currently do this:

http.get<Endereco>('url')
   .subscribe(resposta => this.x = resposta)

Here the variable X becomes an object with the neighborhood parameter, and I do not want that to happen.

    
asked by anonymous 05.12.2018 / 17:51

1 answer

1

You have to map your api response model to your internal application state model, so it is best to use the map operator:

http.get<Endereco>('url')
    .pipe(
         map(resposta=>{
            return {
               resposta.rua,
               resposta.numero
            }
        })
     )
   .subscribe(resposta => this.x = resposta)
    
05.12.2018 / 21:34