【问题标题】:Javascript/GApps Script - Access Object Property using ArrayJavascript/Apps 脚本 - 使用数组访问对象属性
【发布时间】:2014-04-02 15:39:12
【问题描述】:

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

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 语句

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

【问题讨论】:

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


    【解决方案1】:

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

    当您使用.mydate 时,您正在尝试访问属性“mydate”,而使用[mydate] 将尝试访问“first”、“second”、“third”……

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-12-02
      • 2019-10-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-31
      • 2015-06-26
      • 1970-01-01
      相关资源
      最近更新 更多