【问题标题】:Array of Hashes in JavascriptJavascript 中的哈希数组
【发布时间】:2015-04-15 21:50:15
【问题描述】:

我正在使用 Google 表格和脚本来编写从表中查找数据并将记录作为哈希返回的函数。可能有更好的方法来做到这一点,但现在,我正在尝试返回一个记录哈希数组,其中的键是字段名称。

我遇到的问题是从数组中的单个记录哈希中调用一个键。它只是返回“未定义”。这是我编写的测试代码的一部分,用于查看我遇到问题的应用程序代码:

function hashArrayTest() {
  var workbookKey = "I blanked this out"
  var tableName = "AttributeChildParentJoin";
  var headerRow = 1;
  var i;
  var attrsJoinObj = new dbEntitySource(workbookKey, tableName, headerRow);
  var OutputHashArray = attrsJoinObj.returnHashItems("Parent Attribute", "Child Attribute", "AESTHETICS");

  Logger.log("This is the resulting output array of hashes: ");

  for(i=0; i<OutputHashArray.length; i++){
    Logger.log("-------------" + i + "-------------");
    Logger.log(OutputHashArray[i]);
  }

  Logger.log("Here are the Parent Attributes from each hash: ");

  for(i=0; i<OutputHashArray.length; i++){
    Logger.log("-------------" + i + "-------------");
    Logger.log(OutputHashArray[i]['Parent Attribute']);
    }
  }

这是源数据表:




这里是代码输出的日志:

[-] This is the resulting output array of hashes:
[-] -------------0-------------
[-] {'Date Added'='', 'Child Attribute'='Color', 'Parent Attribute'='AESTHETICS', 'Added by'='', 'ID'='21'}
[-] -------------1-------------
[-] {'Date Added'='', 'Child Attribute'='Finish', 'Parent Attribute'='AESTHETICS', 'Added by'='', 'ID'='22'}
[-] -------------2-------------
[-] {'Date Added'='', 'Child Attribute'='Material', 'Parent Attribute'='AESTHETICS', 'Added by'='', 'ID'='23'}
[-] -------------3-------------
[-] {'Date Added'='', 'Child Attribute'='VBL Styling Adherence', 'Parent Attribute'='AESTHETICS', 'Added by'='', 'ID'='24'}
[-] Here are the Parent Attributes from each hash: 
[-] -------------0-------------
[-] undefined
[-] -------------1-------------
[-] undefined
[-] -------------2-------------
[-] undefined
[-] -------------3-------------
[-] undefined

在这个输出中,看起来我已经成功创建了一个哈希数组,尽管我无法从每个哈希中调用 ['Parent Attribute'] 键。

我编写了一个非常简单的函数来测试我的哈希数组问题,但这次我成功了。 =>
代码如下:

function rawHashTest(){
 var recordHash1 = {};
 recordHash1['car'] = 45;
 recordHash1['chicken'] = "ten";

 var recordHash2 = {};
 recordHash2['red'] = 30;
 recordHash2['sticks'] = "blue";

 Logger.log(recordHash1);  
 Logger.log(recordHash1['car']);

 var hashArray = new Array();
 hashArray = [recordHash1, recordHash2];

 Logger.log(hashArray);
 Logger.log(hashArray[0]);
 Logger.log(hashArray[0]["car"]); 
}

这是日志:

[-] {chicken=ten, car=45.0}
[-] 45.0
[-] [{chicken=ten, car=45.0}, {red=30.0, sticks=blue}]
[-] {chicken=ten, car=45.0}
[-] 45.0

我无法弄清楚为什么我能够在我的简单测试代码中从数组中的一个哈希调用哈希键值,但在我测试我的应用程序代码时却不行。我已经尝试了所有不同的方式,并且检查了前导和尾随空格。我在这里想念什么?

谢谢你, 尼古拉斯·金凯德

【问题讨论】:

  • Logger.log(typeof OutputHashArray[0]) 记录什么?你也可以通过OutputHashArray[i]for-in吗?

标签: javascript arrays google-apps-script hash


【解决方案1】:

感谢大家的提问。我检查了我的 returnHashItems 方法并发现了问题。

我的 dbEntitySource.returnHashItems 方法使用另一种方法来构建每个单独的哈希。这是包含原始行和更新行的代码:

 this.returnHash = function returnHash(recordIndex){
    var recordHash = {};

    if (recordIndex !== -1){
      for (i=0; i<this.headerArray.length;i++){

        // This is the line that was giving me the issue:
        //recordHash["'"+ this.headerArray[i] +"'"] = "'" + this.dataArray[recordIndex][i] + "'";          

        // This is the line that fixed the problem:
        recordHash[this.headerArray[i]] = this.dataArray[recordIndex][i];
     }
    }
    return recordHash;
 }

回想起来,原来我以为我必须用引号将它括起来,因为这是直接指定字符串的方法,例如:

 var recordHash1 = {};
 recordHash1['car'] = 45;
 recordHash1['chicken'] = "ten";

但是,由于 headerArray 和 dataArray 中的值已经是字符串,所以这是多余的,也会引起问题。什么类型的问题,我还不确定。

感谢 cssimsek 提供的调试提示。我在修复前后检查了 OutputHashArray[0] 的类型:

 function hashArrayTest() {

  var workbookKey = "1uOHlrW8pSACDg9dNUzMFKgLi8fDBT4Wsqc5KadkoCXk";
  var tableName = "AttributeChildParentJoin";
  var headerRow = 1;
  var i;
  var attrsJoinObj = new dbEntitySource(workbookKey, tableName, headerRow);
  var OutputHashArray = attrsJoinObj.returnHashItems("Parent Attribute", "Child Attribute", "AESTHETICS");

  Logger.log("Here is the data type for the OutputHashArray:");
  Logger.log(typeof OutputHashArray);

  Logger.log("Here is the data type for the OutputHashArray[0]:");
  Logger.log(typeof OutputHashArray[0]);

  Logger.log("This is the resulting output array of hashes: ");

  for(i=0; i<OutputHashArray.length; i++){
   Logger.log("-------------" + i + "-------------");
   Logger.log(OutputHashArray[i]);
  }

  Logger.log("Here are the Parent Attributes from each hash: ");

  for(i=0; i<OutputHashArray.length; i++){
   Logger.log("-------------" + i + "-------------");
   Logger.log(OutputHashArray[i]['Parent Attribute']);
  }
 }



修复前的输出:

[-] 这是 OutputHashArray 的数据类型:
[-] 对象
[-] 这是 OutputHashArray[0] 的数据类型:
[-] 对象
[-] 这是生成的哈希输出数组:
[-] -------------0-------------
[-] {'添加日期'='','子属性'='颜色','父属性'='AESTHETICS','添加者'='','ID'='21'}
[-] -------------1-------------
[-] {'添加日期'='','子属性'='完成','父属性'='AESTHETICS','添加者'='','ID'='22'}
[-] -------------2-------------
[-] {'添加日期'='','子属性'='材料','父属性'='AESTHETICS','添加者'='','ID'='23'}
[-] -------------3-------------
[-] {'添加日期'='','子属性'='VBL 样式遵守','父属性'='AESTHETICS','添加者'='','ID'='24'}
[-] 以下是每个哈希的父属性:
[-] -------------0-------------
[-] 未定义
[-] -------------1-------------
[-] 未定义
[-] -------------2-------------
[-] 未定义
[-] -------------3-------------
[-] 未定义



修复后的输出:

[-] 这是 OutputHashArray 的数据类型:
[-] 对象
[-] 这是 OutputHashArray[0] 的数据类型:
[-] 对象
[-] 这是生成的哈希输出数组:
[-] -------------0-------------
[-] {添加日期=,子属性=颜色,ID=21.0,父属性=AESTHETICS,添加者=}
[-] -------------1-------------
[-] {添加日期=,子属性=完成,ID=22.0,父属性=AESTHETICS,添加者=}
[-] -------------2-------------
[-] {添加日期=,子属性=材料,ID=23.0,父属性=AESTHETICS,添加者=}
[-] -------------3-------------
[-] {添加日期=,子属性=VBL 样式遵守,ID=24.0,父属性=AESTHETICS,添加者=}
[-] 以下是每个哈希的父属性:
[-] -------------0-------------
[-] 美学
[-] -------------1-------------
[-] 美学
[-] -------------2-------------
[-] 美学
[-] -------------3-------------
[-] 美学

看起来问题不在于我的 hashArray 的数据类型,而在于我如何指定哈希键。谢谢大家的调试技巧,真的很有帮助!

【讨论】:

    猜你喜欢
    • 2019-05-21
    • 1970-01-01
    • 1970-01-01
    • 2011-03-14
    • 2012-06-19
    • 1970-01-01
    • 2020-07-24
    • 2017-10-02
    • 2020-07-19
    相关资源
    最近更新 更多