PDA

View Full Version : Is there a better way?



unflores
Oct 19th, 2009, 04:12 PM
Hello all,
I am doing something with google maps api and I'm checking to see if the


I have a long json structure. I want to see if the Thoroughfare element is 'undefined'. That is I want to do something like this:


if(typeof(response.Placemark[0].AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality.Thoroughfare) != 'undefined'){
//do something awesome
}


However, the json object can be 'undefined' at any point along the structure. My script stops if say AddressDetails
is 'undefined'. Is there anyway to only check if Thoroughfare is 'undefined' without having javascript **** a brick?



Thanks All,

-Austin

Philip M
Oct 19th, 2009, 04:33 PM
Try this:-


if((typeof(response.Placemark[0].AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality.Thoroughfare) == 'undefined') && (typeof(response.Placemark[0].AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality) != 'undefined')){
// Thoroughfare is undefined
}

Quizmaster: In which European country is the Neander valley?
Contestant: Africa

unflores
Oct 19th, 2009, 05:32 PM
So, this works sometimes but there is a chance that any of the elements of the json object will be undefined. My current understanding is that I have to check each element before I test the last element Thoroughfare.

It would be awesome if there were something in js that would allow me to just test

response.Placemark[0].AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality.Thoroughfare

and not worry about all of the elements before it in the object.


-Thanks for your help though.

Philip M
Oct 19th, 2009, 05:39 PM
So, this works sometimes but there is a chance that any of the elements of the json object will be undefined. My current understanding is that I have to check each element before I test the last element Thoroughfare.




As I see it the code I gave you will return true if and only if Thoroughfare and no other element is undefined, but perhaps I am not sufficiently familiar with json objects.

Old Pedant
Oct 19th, 2009, 10:11 PM
You *could* always wrap the code in a try...catch... block, you know.

Philip M
Oct 19th, 2009, 11:02 PM
You *could* always wrap the code in a try...catch... block, you know.

How would that identify that Thoroughfare and only Thoroughfare was undefined?

Old Pedant
Oct 20th, 2009, 02:01 AM
Oh...doh on me.

Teach me not to read thoroughly.

unflores
Oct 20th, 2009, 08:20 AM
Not sure if my initial question was posed as clearly as it could have been but the try catch block did the trick. I ended up using an if statement to check Thoroughfare and if I catch an error then one of it's parents along the way were undefined.


Worked out great. Thanks all.

Old Pedant
Oct 20th, 2009, 09:40 PM
LOL! I'm smarter than I look. (Hmmm...come to think of it, that's not too hard.)

Philip M
Oct 21st, 2009, 09:19 AM
I still do not see how try..catch would identify that Thoroughfare and only Thoroughfare was undefined, or that Thoroughfare was defined but one of its parents was not.

Old Pedant
Oct 21st, 2009, 09:18 PM
It's obvious when all is said and done!



<script>
function objA( b )
{
this.B = b;
}
function objB( c )
{
this.C = c;
}
function objC( d )
{
this.D = d;
}

var A = new objA( new objB ( new objC(null) ) );

var found = false;
try
{
found = ( A.B.C.D != null );
alert( "D is " + ( found ? "existent" : "null" ) );
}
catch( unused )
{
alert( "A or B or C non-existent" );
}

var A2 = new objA( new objB( null ) );
var found2 = false;
try
{
found2 = ( A2.B.C.D != null );
alert( "D is " + ( found2 ? "existent" : "null" ) );
}
catch( notused )
{
alert( "A2 or B or C non-existent" );
}
</script>


You see it now?

In this line:

found = ( A.B.C.D != null );

A and B and C are all existent members (and objects) and so we really are simply testing if the D member there is null.

But in this line:


found2 = ( A2.B.C.D != null );

A2 and B exist, but C is null. And we can't find a member (".D") of a null object and so the exception is thrown!

It's a great solution. But not exactly what I was thinking of when I suggested using try...catch. So that's why I laughed at myself for getting the right answer kind of by accident.

***************

Note: If you change that "var A" line to this:

var A = new objA( new objB ( new objC( "test" ) ) );
then of course the alert says "D is existent", just as you would want.

Old Pedant
Oct 21st, 2009, 09:45 PM
Now, it does *NOT* tell you *which* of A2 or B or C is null, of course. But I don't think that was needed for his answer.