traversing an object with Angular 5

0

I have the following, I receive data in JSON format from a base in firebase using the following code:

  constructor(db: AngularFirestore, private modalService: BsModalService) {
    db.collection('usuarios').valueChanges()
             .subscribe( ( data: Taxista[] ) => {

             this.taxistas = data;
             console.log('data->' + JSON.stringify(data));

    });
}

in the HTML I use * ngFor to traverse the json:

 <div class="list-group">
  <a href="#"  *ngFor="let taxista of taxistas" class="list-group-item list-group-item-action">
    <strong> {{ taxista.nombre }} </strong><br>

This is the JSON format I receive from firebase:

[
{
"1": {
  "demandado": "JOSE CARLOS PIÑOL PLANA",
  "direccion": "Barlovento 1, Real Solare.",
  "fecha_asignada": "2018-05-01 00:00:00",
  "id_expediente": "1",
  "lat": 20.6075857,
  "lng": -100.3773038,
  "status": "Inicio",
  "tipo_juicio": "1"
},
"2": {
  "demandado": "JUAN MANUEL BELMONTE HERMIDA",
  "direccion": "Barlovento 1, Real Solare",
  "fecha_asignada": "2018-06-14 00:00:00",
  "id_expediente": "2",
  "lat": "",
  "lng": "",
  "status": "",
  "tipo_juicio": "1"
},
"3": {
  "demandado": "JORGE AVILA BERRIO",
  "direccion": "Barlovento 1, Real Solare.",
  "fecha_asignada": "2018-05-01 00:00:00",
  "id_expediente": "3",
  "lat": "",
  "lng": "",
  "status": "",
  "tipo_juicio": "1"
}
}
]      

First problem is the JSON Id (it's incremental), sometimes more data can be, how can I go through this in the HTML?, I imagine something like this {{taxi driver [id] .address}}

    
asked by Eze 26.06.2018 в 23:57
source

2 answers

0

*ngFor="let taxista of taxistas; let i = index" and you could run the json in the following way: {{ taxista[i].direccion }} .This could be a way.

    
answered by 13.07.2018 в 15:32
0

The JSON that you are creating in firebase, should have the id inside the object, because there you have an array of size 3, with these values [1,2,3], in which each value within it has a taxi driver, the format you should have is the following:

[
  {
  "id": 1
  "demandado": "JOSE CARLOS PIÑOL PLANA",
  "direccion": "Barlovento 1, Real Solare.",
  "fecha_asignada": "2018-05-01 00:00:00",
  "id_expediente": "1",
  "lat": 20.6075857,
  "lng": -100.3773038,
  "status": "Inicio",
  "tipo_juicio": "1"
   },
  "id": 2
  "demandado": "JUAN MANUEL BELMONTE HERMIDA",
  "direccion": "Barlovento 1, Real Solare",
  "fecha_asignada": "2018-06-14 00:00:00",
  "id_expediente": "2",
  "lat": "",
  "lng": "",
  "status": "",
  "tipo_juicio": "1"
   },
  "id": 3
  "demandado": "JORGE AVILA BERRIO",
  "direccion": "Barlovento 1, Real Solare.",
  "fecha_asignada": "2018-05-01 00:00:00",
  "id_expediente": "3",
  "lat": "",
  "lng": "",
  "status": "",
  "tipo_juicio": "1"    
   }
]
    
answered by 13.07.2018 в 15:46