Reputation: 2107
I've tried everything I can think of to test if an object exists in this json array.
var estimatedGateDeparture = typeof data.flightStatuses[i].operationalTimes.estimatedGateDeparture.dateLocal;
I know that this array is empty. my code just stops running when it encouters the above line. I've tried to test if the key is "in" the object. Still dies. it seems to me that if I use "typeof", that that would be a REALLY safe way to see what's going on. Still dies.
this still doesn't work. It catches when nothing is there, but when something IS there, it doesn't "see" it.
if (data.flightStatuses[i].operationalTimes.length > 0)
{var estimatedGateDeparture = data.flightStatuses[i].operationalTimes.estimatedGateDeparture.dateLocal;
}
else
{alert ("no statuses");
}
Subset of Data: So, I'm trying to pull out .estimatedGateDeparture.dateLocal
{"flightId":321439750,"carrierFsCode":"WN","flightNumber":"963","departureAirportFsCode":"BWI","arrivalAirportFsCode":"CHS","departureDate":{"dateLocal":"2014-01-12T16:05:00.000","dateUtc":"2014-01-12T21:05:00.000Z"},"arrivalDate":{"dateLocal":"2014-01-12T17:40:00.000","dateUtc":"2014-01-12T22:40:00.000Z"},"status":"S","schedule":{"flightType":"J","serviceClasses":"RY","restrictions":"F","uplines":[{"fsCode":"MDW","flightId":321477764},{"fsCode":"CLE","flightId":321444143},{"fsCode":"BNA","flightId":321436891}]},"operationalTimes":{"publishedDeparture":{"dateLocal":"2014-01-12T16:05:00.000","dateUtc":"2014-01-12T21:05:00.000Z"},"publishedArrival":{"dateLocal":"2014-01-12T17:40:00.000","dateUtc":"2014-01-12T22:40:00.000Z"},"scheduledGateDeparture":{"dateLocal":"2014-01-12T16:05:00.000","dateUtc":"2014-01-12T21:05:00.000Z"},"estimatedGateDeparture":{"dateLocal":"2014-01-12T16:05:00.000","dateUtc":"2014-01-12T21:05:00.000Z"},"flightPlanPlannedDeparture":{"dateLocal":"2014-01-12T16:15:00.000","dateUtc":"2014-01-12T21:15:00.000Z"},"estimatedRunwayDeparture":{"dateLocal":"2014-01-12T16:15:00.000","dateUtc":"2014-01-12T21:15:00.000Z"},"scheduledGateArrival":{"dateLocal":"2014-01-12T17:40:00.000","dateUtc":"2014-01-12T22:40:00.000Z"},"estimatedGateArrival":{"dateLocal":"2014-01-12T17:40:00.000","dateUtc":"2014-01-12T22:40:00.000Z"},"flightPlanPlannedArrival":{"dateLocal":"2014-01-12T17:35:00.000","dateUtc":"2014-01-12T22:35:00.000Z"},"estimatedRunwayArrival":{"dateLocal":"2014-01-12T17:35:00.000","dateUtc":"2014-01-12T22:35:00.000Z"}},"codeshares":[{"fsCode":"FL","flightNumber":"2963","relationship":"L"}],"flightDurations":{"scheduledBlockMinutes":95,"scheduledAirMinutes":80,"scheduledTaxiOutMinutes":10,"scheduledTaxiInMinutes":5},"airportResources":{"departureGate":"A3","arrivalGate":"B5"},"flightEquipment":
Upvotes: 1
Views: 157
Reputation: 32912
Somewhere on your path is undefined
, i.e.
var x;
var y=0;
console.log(x.a); // error: cannot read property 'a' of undefined
console.log(y.a); // undefined, no error
console.log(z.a); // error: 'z' is not defined
Therefore, you should test all the object on the path
var x, estimatedGateDeparture=null;
if(
(x=data) &&
(x=x.flightStatuses) &&
(x=x[i]) &&
(x=x.operationalTimes) &&
(x=x.estimatedGateDeparture)
) estimatedGateDeparture = x.dateLocal;
or, if the performance isn't an issue and if you are lazy, just use exceptions:
var estimatedGateDeparture;
try { estimatedGateDeparture = data.flightStatuses[i].operationalTimes.estimatedGateDeparture.dateLocal; }
catch(e) { estimatedGateDeparture = null; }
Upvotes: 1
Reputation: 133
You can try checking value returned by your object's "constructor" field:
var x = new User();
typeof x ==> returns "object"
x.constructor ==> returns "User"
I know you have an array but you can also use this method instead of typeof operator.
Upvotes: 0
Reputation: 6414
Okay, I think what you want to do is this
if (data.flightStatuses.length > i){
var estimatedGateDeparture = data.flightStatuses[i].operationalTimes.estimatedGateDeparture.dateLocal;
}
Upvotes: 0