0

Okay, I asked a generic question before. Let me be more specific.

I have this data set in JSON, which contains many nested objects and arrays.

Here is a sample:

"data": [
    {
        "attributes": {
            "type": "analysis"
        },
        "disturbance": {
            "attributes": {
                "ID": "1861111306_114S_793E"
            },
            "cycloneName": "XXXX861056",
            "fix": [
                {
                    "validTime": "1861-11-13T06:00:00",
                    "latitude": "-11.40",
                    "longitude": "  79.30",
                    "cycloneData": {
                        "minimumPressure": {
                            "pressure": "   0.0"
                        },
                        "maximumWind": {
                            "speed": "  0.0",
                            "averagingPeriod": "10"
                        }
                    }
                },
                {
                    "validTime": "1861-11-14T06:00:00",
                    "latitude": "-13.00",
                    "longitude": "  73.90",
                    "cycloneData": {
                        "minimumPressure": {
                            "pressure": "   0.0"
                        },
                        "maximumWind": {
                            "speed": "  0.0",
                            "averagingPeriod": "10"
                        }
                    }
                },

I can iterate through and get all of the unique IDs using:

console.log(json.data[i].disturbance.attributes.ID);

When I try to do through and get the values of all of the latitude and longitude pairs in the array fix I get only the values for the first instance of the object "disturbance" and this error

Uncaught TypeError: Cannot read property 'latitude' of undefined

I am iterating though using this:

var l = $(json.data[i].disturbance.fix).length;
    console.log(l);
    for(h=0;h<=l;h++) {

console.log(json.data[i].disturbance.fix[h].latitude, json.data[i].disturbance.fix[h].longitude);

    }

What's going on? I'm really unclear how I can get every lat/long in each array fix (there are 14 total in this set of data). Each fix[] has multiple unnamed objects that contain lat/long and other data objects.

Can someone point me in the right direction?

Here is my complete javascript:

$.getJSON("hurricane-json.json", function(json) {
$.each(json, function(){
var d = $(json.data).length;
for(i=0; i<=d;i++){
    console.log(i);
    console.log(json.data[i].disturbance.attributes.ID );
    var l = $(json.data[i].disturbance.fix).length;
    console.log(l);
    for(h=0;h<=l;h++) {

console.log(json.data[i].disturbance.fix[h].latitude, json.data[i].disturbance.fix[h].longitude);

    }
    }
});

});

Thanks!

0 Answers0