Javascript / GApps脚本 - 使用Array访问对象属性

时间:2014-04-02 15:39:12

标签: javascript arrays object properties google-apps-script

我有一个对象,最多可能有三十个属性,需要多次检查所有值。所以我创建了一个包含这些属性名称的数组,这样就不必将每个属性分配给它自己的变量,我可以使用数组中的循环来检查这个属性的值。

var dates = ["first", "second", "third", "fourth", "fifth", "sixth", "seventh", "eighth", "ninth", "tenth", "eleventh", "twelfth", "thirteenth", "fourteenth", "fifteenth", "sixteenth", "seventeenth", "eighteenth", "nineteenth", "twentieth", "twentyfirst", "twentysecond", "twentythird", "twentyfourth", "twentyfifth", "twentysixth", "twentyseventh", "twentyeighth", "twentyninth", "thirtieth", "thirtyfirst"];

for (var k=3; k<=lastrw; k++){
for (var l = 0; l < 31; l++){
  var mydate = dates[l];
  if (typeof holidayObjects[k-3].mydate != undefined){
    switch (holidayObjects[k-3].mydate){
      case "HD": break;
      case "H": break;
      case "BH": break;
      case "": break;
      case undefined: break;
      default: ss.getSheetByName(mysheet).getRange(k,(l+2)).setValue("");
    }
  }
}
}

然而,这似乎永远不会起作用,请记住,某些属性永远不会被定义,例如它可能首先定义但第三个未定义,因此if语句

是否有任何理由说明为什么这不起作用,如果是,我将如何解决这个问题?为什么?

1 个答案:

答案 0 :(得分:0)

使用holidayObjects[k-3][mydate]代替holidayObjects[k-3].mydate

当您使用.mydate时,您尝试访问属性“mydate”,因为使用[mydate]将尝试访问“first”,“second”,“third”,...