gpt4 book ai didi

javascript - JavaScript 中的哈希数组

转载 作者:行者123 更新时间:2023-11-28 07:25:52 25 4
gpt4 key购买 nike

我正在使用 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']);
}
}

这是源数据表:

An image of the data table source


以下是代码输出的日志:

[-] 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

我不明白为什么我能够在简单的测试代码中从数组中的哈希值之一调用哈希键值,但在测试应用程序代码时却不能。我尝试了所有不同的方法,并检查了前导空格和尾随空格。我在这里缺少什么?

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

最佳答案

谢谢大家的提问。我检查了 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 中的值已经是字符串,因此这是多余的并且也导致了问题。什么类型的问题,我还不太确定。

谢谢 csssimsek 提供的调试技巧。我检查了修复前后 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-------------
[-] {'添加日期'='','子属性'='颜色','父属性'='美学','添加者'='','ID'='21'}
[-] -------------1-------------
[-] {'添加日期'='','子属性'='完成','父属性'='美学','添加者'='','ID'='22'}
[-] -------------2-------------
[-] {'添加日期'='','子属性'=' Material ','父属性'='美学','添加者'='','ID'='23'}
[-] -------------3-------------
[-] {'添加日期'='','子属性'='VBL 样式遵循','父属性'='AESTHETICS','添加者'='','ID'='24'}
[-] 以下是每个哈希的父属性:
[-] -------------0-------------
[-] 未定义
[-] -------------1-------------
[-] 未定义
[-] -------------2-------------
[-] 未定义
[-] -------------3-------------
[-] 未定义



修复后的输出:

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

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

关于javascript - JavaScript 中的哈希数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29661564/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com