How could I traverse an array of a JSON file?

4

I want to go through it with a loop for in JavaScript.

let jsonObject =   {
        "hoteles": [{
            "viaje": {
                "origen": {
                    "iataCode": "VL",
                    "name": "Valencia",
                    
                },
                "destino": {
                    "iataCode": "BCN",
                    "name": "Barcelona",
                },
                "price": {
                    "value": "99.16",
                    "valueMainUnit": "99",
                    "valueFractionalUnit": "16",
                    "currencySymbol": "€"
                },
                "dateFrom": "2015-02-02T00:00:00+00:00",
                "dateTo": "2015-04-02T00:00:00+01:00"
            }
        },
		{
            "viaje": {
                "origen": {
                    "iataCode": "VL",
                    "name": "madrid",
                    
                },
                "destino": {
                    "iataCode": "BCN",
                    "name": "Barcelona",
                },
                "price": {
                    "value": "99.16",
                    "valueMainUnit": "99",
                    "valueFractionalUnit": "16",
                    "currencySymbol": "€"
                },
                "dateFrom": "2015-02-02T00:00:00+00:00",
                "dateTo": "2015-04-02T00:00:00+01:00"
            }
		}
		]
	}


    for (var i=0; i< jsonObject.hoteles.length; i++)
    {
        //Para obtener el objeto de tu lista
        var hotel = jsonObject.hoteles[i]
        //Mostramos el objeto en su versión String
        console.log(JSON.stringify(hotel));
        //Muestras el valor de la propiedad name para el objeto viaje, del objeto hotel.
        document.write(console.log(hotel.viaje.origen.name));
    }

When traversing it, why do you print twice undefined ? How could I go through this array?

    
asked by gabriel gomez 25.05.2016 в 17:44
source

6 answers

3

You only have to go through the array of the hotels property of your object with a for:

var jsonObject = {
    "hoteles": [{
        "viaje": {
            "origen": {
                "iataCode": "VL",
                "name": "Valencia",

            },
            "destino": {
                "iataCode": "BCN",
                "name": "Barcelona",
            },
            "price": {
                "value": "99.16",
                "valueMainUnit": "99",
                "valueFractionalUnit": "16",
                "currencySymbol": "€"
            },
            "dateFrom": "2015-02-02T00:00:00+00:00",
            "dateTo": "2015-04-02T00:00:00+01:00"
        }
    },
    {
        "viaje": {
            "origen": {
                "iataCode": "VL",
                "name": "madrid",

            },
            "destino": {
                "iataCode": "BCN",
                "name": "Barcelona",
            },
            "price": {
                "value": "99.16",
                "valueMainUnit": "99",
                "valueFractionalUnit": "16",
                "currencySymbol": "€"
            },
            "dateFrom": "2015-02-02T00:00:00+00:00",
            "dateTo": "2015-04-02T00:00:00+01:00"
        }
    }
    ]
};

for (var i=0; i< jsonObject.hoteles.length; i++){
  console.log(JSON.stringify(jsonObject.hoteles[i]));
  console.log('origen viaje: ' + jsonObject.hoteles[i].viaje.origen.name);
}
    
answered by 25.05.2016 в 17:58
3

In relation to the code that you put, you could use something similar to this:

for (var i=0; i< jsonObject.hoteles.length; i++)
{
    //Para obtener el objeto de tu lista
    var hotel = jsonObject.hoteles[i];
    //Mostramos el objeto en su versión String
    console.log(JSON.stringify(hotel));
    //Muestras el valor de la propiedad name para el objeto viaje, del objeto hotel.
    console.log(hotel.viaje.origen.name)
}

I hope I have helped you.

Greetings.

    
answered by 25.05.2016 в 18:08
2
  

You can use the Map method to easily navigate and save yourself a   for

let hoteles = jsonObject.hoteles
hoteles.map((hotel) => { console.log(hotel) })

I hope it serves you

    
answered by 01.11.2017 в 16:07
2

You have two errors in the exercise

  • You must declare the variable and then go through the for
  • the console.log function returns no value! only print values.
  •   

    The console.log prints the values that are inside the parenthesis, but it does not return any value, therefore, when placing document.write(console.log("valor")) ; you are actually printing the return of the function that is undefined

    function mantequilla(){
      console.log("chispas");
        return "chispas";
    }
    function mantequilla2(){
      
        return console.log("va a imprimir el valor pero retornara undefined");
    }
    
    document.write(mantequilla());
    
    document.write(mantequilla2());

    As you can see in this example I made a console.log () but it did not show it! in the html it is the equivalent of what you want to do.

    Your schedule should change to something like this:

    let jsonObject = {
        "hoteles": [{
            "viaje": {
                "origen": {
                    "iataCode": "VL",
                    "name": "Valencia",
    
                },
                "destino": {
                    "iataCode": "BCN",
                    "name": "Barcelona",
                },
                "price": {
                    "value": "99.16",
                    "valueMainUnit": "99",
                    "valueFractionalUnit": "16",
                    "currencySymbol": "€"
                },
                "dateFrom": "2015-02-02T00:00:00+00:00",
                "dateTo": "2015-04-02T00:00:00+01:00"
            }
        },
        {
            "viaje": {
                "origen": {
                    "iataCode": "VL",
                    "name": "madrid",
    
                },
                "destino": {
                    "iataCode": "BCN",
                    "name": "Barcelona",
                },
                "price": {
                    "value": "99.16",
                    "valueMainUnit": "99",
                    "valueFractionalUnit": "16",
                    "currencySymbol": "€"
                },
                "dateFrom": "2015-02-02T00:00:00+00:00",
                "dateTo": "2015-04-02T00:00:00+01:00"
            }
        }
        ]
    }
    for (var i=0; i< jsonObject.hoteles.length; i++)
    {
        //Para obtener el objeto de tu lista
        var hotel = jsonObject.hoteles[i]
        //Mostramos el objeto en su versión String
        console.log(JSON.stringify(hotel));
        //Muestras el valor de la propiedad name para el objeto viaje, del objeto hotel.
        document.write((hotel.viaje.origen.name));
    }

    Please do not use document.write to print on the page

        
    answered by 13.12.2017 в 17:38
    2

    You get undefined because you reference an attribute that does not exist:

    hotel.viaje.origen.name // no existe pues no se ha indicado el índice del arreglo hotel
    

    One way you can review your arrangement might be like this:

    for (var i=0; i<jsonObject.hoteles.length; i++) {
      var hotel = jsonObject.hoteles[i]; // hasta aquí tienes cada hotel
      for (var detalle in hotel.viaje) {
        for (var dato in hotel.viaje[detalle]) {
           console.log (hotel.viaje[detalle][dato]); // Y aquí exploras cada dato del viaje
        }
      }
    }
    

    You can check this fiddle . Greetings.

        
    answered by 27.02.2017 в 18:25
    1

    You should assign the information to a variable.

    var variable = { // tus datos };
    
    for(i = 0; i < variable.hoteles.length; i++){
       var datos = variable.hoteles[i].viaje;
       console.info(datos.origen);
       console.info(datos.destino);
       console.info(datos.precio);
    }
    
        
    answered by 25.05.2016 в 18:18