javascript - How to tell if a property in a sub-object exists? -


i have object defined as

{     "query" :     {         /* snip */     },     "aggs":     {         "times" :         {             "date_histogram" :             {                 "field" : "@timestamp",                 "interval" : "15m",                 "format" : "hh:mm",                 "min_doc_count" : 0             }         }     } }; 

how can tell whether interval in aggs.times.date_histogram exists, can manipulate it?

clarification: can not sure of parent objects interval exist.

assuming value non-blank string, test truthiness:

if (aggs.times.date_histogram.interval) {     // use } 

you might cache result of property lookups. though it's unlikely matter performance, may useful code maintainability:

var interval = aggs.times.date_histogram.interval; if (interval) {     // use } 

if need worry each level may not exist, gets more verbose:

if (aggs &&     aggs.times &&     aggs.times.date_histogram &&     aggs.times.date_histogram.interval) {     // use } 

there's a question several answers writing function that.


Comments

Popular posts from this blog

Magento/PHP - Get phones on all members in a customer group -

php - .htaccess mod_rewrite for dynamic url which has domain names -

Website Login Issue developed in magento -