Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/411.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 允许Google脚本跳过未定义的数组_Javascript_Arrays_Google Sheets - Fatal编程技术网

Javascript 允许Google脚本跳过未定义的数组

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

我的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.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只会排除该节点。