我有一种情况需要确定对象是否为空。我无法弄清楚if语句是否有效。
function test_isObjectEmpty(){
var responces = {};
// var responces = {'test':'test'};
var result = "";
if (responces == {}){ // this does not work
result ="Nothing found";
} else {
result ="Responce found";
}
Logger.log(result)
}
答案 0 :(得分:1)
您可以检查对象长度,如果为0,则为空:
var obj = {};
if (Object.getOwnPropertyNames(obj).length === 0) {
//it's empty
}
else {
//it's not empty
}
答案 1 :(得分:0)