Javascript 允许Google脚本跳过未定义的数组
我的GoogleSheets脚本中有一堆数组,其中一些包含shares.count;然而,有些人没有Javascript 允许Google脚本跳过未定义的数组,javascript,arrays,google-sheets,Javascript,Arrays,Google Sheets,我的GoogleSheets脚本中有一堆数组,其中一些包含shares.count;然而,有些人没有 feeds.getRange(j+2,8+(i*7),1,1).setValue(response_json.data[j].shares.count); 不幸的是,对于那些不这样做的人来说,这会引发一个错误,我已经尝试在以下内容中编写了一个if语句: if(response_json.data[j].shares.count != 'undefined') {feeds.getRa
feeds.getRange(j+2,8+(i*7),1,1).setValue(response_json.data[j].shares.count);
不幸的是,对于那些不这样做的人来说,这会引发一个错误,我已经尝试在以下内容中编写了一个if语句:
if(response_json.data[j].shares.count != 'undefined')
{feeds.getRange(j+2,8+(i*7),1,1).setValue(response_json.data[j].shares.count);}
然而,我仍然没有得到运气
让Google脚本识别缺失数组或未定义数组的最佳方法是什么
谢谢Un您得到未定义错误的部分是什么?Cause shares.count表示一个可能为0的数字。在不知道存在什么和不存在什么的情况下,您可以在尝试修改属性链之前检查属性链是否存在,只是为了安全起见
if(response_json && response_json.data[j] && response_json.data[j].shares && response_json.data[j].shares.count) {
// now do something
}
有些数组包含shares.count,有些根本不包含。有趣的是,如果是0,json只会排除该节点。