我定义了以下变量:
var locationObj = {
latitude: result.geometry.location.lat,
longitude: result.geometry.location.lng,
streetNumber: _.find(addComp, function (comp){return comp.types.indexOf('street_number') > -1}).long_name || '',
route: _.find(addComp, function (comp){return comp.types.indexOf('route') > -1}).long_name || '',
htmlAddress: result.adr_address,
address: result.formatted_address,
vicinity: result.vicinity,
modifiedAt: Date.now()
}
问题是,comp
对象可能为null,如果在comp
为空,我想在var对象中写一个内容,将属性值设置为空。我该怎么做?
答案 0 :(得分:3)
你可以这样做:
return comp ? comp.types.indexOf('route') > -1 : false;
如果comp为false
或null
undefined