How do I work with Arrays using Angular 5 in the log console?

Asked

Viewed 100 times

0

I’m trying to implement a Multiselect Dynamic dropdown on Angular, it’s like it’s selecting state according to cities, if I select for example PE in the first dropdown list to it will return me automatically in the dropdown list the list of all the cities of Pernambuco if I happen to select SP in the first dropdown list then it will return me in the second dropdown list the list of all city of São Paulo and so on.

As I have little experience and do not know how to implement this, I am trying to make this implementation into pieces.

See the code below:

 listarTodas(): Promise<any> {
    return this.http.get(this.cidadesUrl)
      .toPromise()
      .then(response => console.log(response.json()));
  }

This is the return in json:

inserir a descrição da imagem aqui

  • I’d like to return at console browser only the attributes of code
  • I would also like to return at console browser only code code 1

For me it is important to know these things because when I start to understand these principles of implementation I will be able to start implementing dropdown Multiselect

  • You want to print on the console only the records that have codigoEstado === 1? In the example given, it would be {codigo: 1, nome: "Rio Branco", codigoEstado: 1}, {codigo: 2, nome: "Cruzeiro do Sul", codigoEstado: 1}

  • first I want to thank you for helping me, but this is exactly what I need!

1 answer

1


Use the function filter

listarTodas(): Promise<any> {
    return this.http.get(this.cidadesUrl)
      .toPromise()
      .then(response => {
          let resultado = response.json();
          console.log(resultado.filter(_ => _.codigoEstado === 1));
      });
}

See working below:

const resultado = [
  { codigo: 1, nome: "Rio Branco", codigoEstado: 1 },
  { codigo: 2, nome: "Cruzeiro do Sul", codigoEstado: 1 },
  { codigo: 3, nome: "Salvador", codigoEstado: 2 },
  { codigo: 4, nome: "Porto Seguro", codigoEstado: 2 }
];

console.log(resultado.filter(_ => _.codigoEstado === 1));

  • do not follow do recognize the json()

  • @wladyband like that, it didn’t work?

  • I already understood why it didn’t work, you put the value of the result as a constant and made an example that worked, but it’s not your fault, it was what I had asked in the announcement, but that actually my need was different, it’s because I expressed myself badly. I only know I’m lost.

  • I don’t know who to turn to and I still have a question that’s simple, but so far there hasn’t been a charitable soul to help me, but rest assured, I’ll stay here trying until I get it.

Browser other questions tagged

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