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

0

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

As I have little experience and I do not know how to implement this, I'm trying to do this implementation in chunks.

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:

  • I would like to return in the console of the browser only the attributes of the StatusCode
  • I would also like to return in the console of the browser only code that are 1

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

    
asked by anonymous 05.02.2018 / 10:46

1 answer

1

Use the 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));
    
05.02.2018 / 17:46