0

Just wanted to ask what is best practise for running multiple javascript checks on a returned object from a api.

For instance if the api returns this json.

 var ob = {
       "destination_addresses" : [ "15 Duke St, Cardiff, Cardiff CF10, UK" ],
       "origin_addresses" : [ "2 College St, Swansea, Swansea SA1 5AE, UK" ],
       "rows" : [
          {
             "elements" : [
                {
                   "distance" : {
                      "text" : "67.9 km",
                      "value" : 67941
                   },
                   "duration" : {
                      "text" : "57 mins",
                      "value" : 3446
                   },
                   "status" : "OK"
                }
             ]
          }
       ],
       "status" : "OK"
    }

So say i want check if duration exists only but i don't want to do the following.

if(ob.rows){
  if(ob.rows[0].elements){
     if(ob.rows[0].elements[0].duration.text != undefined ){
         console.log(ob.rows[0].elements[0].duration.text);
     }  
  }
}

Is there a better way to just check in duration and text exists?

https://jsbin.com/turarurula/edit?js,console

user1503606
  • 3,872
  • 13
  • 44
  • 78
  • Are there any reasons not to use a try-catch block? – Gabriel Câmara Nov 17 '15 at 11:50
  • @GabrielCâmara Generally if you're able to test for and prevent an exception it's better than letting one happen for purposes of flow control. However, if you are expecting that this JSON _always_ contains such a property, then you could say that not having it _is exceptional_ and in that instance, yes it would be appropriate to use `try`/`catch`. – James Thorpe Nov 17 '15 at 11:53
  • @JamesThorpe, good point. I was actually thinking in the last case, but, yes, avoid an exception is better then let it happen. – Gabriel Câmara Nov 17 '15 at 11:55
  • Thanks seen the duplicate now, so there isnt really a neater way of doing it without having another extra function? – user1503606 Nov 17 '15 at 12:01
  • Not really - there are other alternative syntaxes you can use with inline blank objects etc - see the linked question. I don't think there's anything in ES2015 that makes it any easier either. – James Thorpe Nov 17 '15 at 12:12

1 Answers1

2

I use this function in all my projects

function verifyObject( objString, objObject ) {
    var objElements = objString.split( "." );
    var tempObj = objObject;
    for( var i = 0; i < objElements.length; i++ ) {
        var objKey = objElements[ i ];
        if( !tempObj[objKey] ) {
            return false;
        }
        tempObj = tempObj[objKey];
    }
    return true;
}

You simply need to call it this way

verifyObject( "rows.0.duration" , ob );

And it will return FALSE because "ob.rows[0].duration" doesn't exist

verifyObject( "rows.0.elements.0.duration" , ob )

Will return TRUE because "ob.rows[0].elements[0].duration" exists. If you want to get the value of ob.rows[0].elements[0].distance.text

if( verifyObject( "ob.rows.0.elements.0.distance.text", ob ) ) {
   var valueVar = ob.rows[0].elements[0].distance.text;
} else {
   // VALUE DOESN'T EXIST
}
RiccardoC
  • 876
  • 5
  • 10