如何确定对象是否为空

时间:2014-10-22 15:28:37

标签: google-apps-script

我有一种情况需要确定对象是否为空。我无法弄清楚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)
}

2 个答案:

答案 0 :(得分:1)

您可以检查对象长度,如果为0,则为空:

var obj = {};
if (Object.getOwnPropertyNames(obj).length === 0) {
  //it's empty
}
else {
  //it's not empty
}

答案 1 :(得分:0)