My problem is this:
I have an end point that will return JSON containing a structure named: the_data_i_want
If the server detects there is a value for the_data_i_want then it returns something like this:
{
"sample": {
"title": "The Title",
"foo": {
"Bar": {
"Baz": {
"Qux": {
"the_data_i_want": "DATA PARTY!!!!"
}
}
}
}
}
}
If the server detects that the_data_i_want is empty, the JSON it returns looks something like this:
{
"sample": {
"title": "The Title"
}
}
My problem is that because any segment of the chain sample.foo.Bar.Baz.Qux.the_data_i_want my be undefined i end up doing these Crazy chains of checks on each step through the json structure where i look and first see if it’s undefined, then if it isn’t, i try the next one and next one until i get to the_data_i_want and it seems wrong.
The question:
Is there a “correct” way to handle this? What would i do in the even the nesting is arbitrarily deep? I’m open to using jQuery.
thank you!
May be this might work.. But there may be better methods out there..
In this approach I have all the keys in an array and check them in a loop..
Check Fiddle