How to work with arrays at Angular 5

Asked

Viewed 201 times

0

I’m taking the list like this:

listarTodosEstados() {
    return this.http.get<any[]>(`${this.estadosUrl}`);
  }

This is my Component:

pesquisarEstados() {
    this.cidadesService.listarTodosEstados()
      .subscribe(dados => {
        console.log(this.estados = dados);
      });
  }

That is the result:

inserir a descrição da imagem aqui

Please, I would like to know how to only print on the console.log only the attribute code?

1 answer

1


Use the function map

pesquisarEstados() {
    this.cidadesService.listarTodosEstados().subscribe(dados => {
        this.estados = dados;
        console.log(this.estados.map(() => <any>e.codigo));
    });
}

See an example

const estados = [{codigo: 1, nome:'Rio Grande do Sul'}, 
                 {codigo: 2, nome:'São Paulo'}, 
                 {codigo: 3, nome:'Minas Gerais'}];

console.log(estados.map((e) => e.codigo));

  • thank you very much for the suggestion :)

  • there’s only one detail, he didn’t recognize the e.codigo because it didn’t even compile.

  • @wladyband It is because you typed the list as any, right. Just put any before.

  • 1

    sorry my lack of understanding, I copied and pasted the way your suggestion is, where I will put any?

  • 1

    @wladyband is there at the beginning of the answer, young man

  • found, valeu hahaha

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.