gpt4 book ai didi

javascript - 为什么我无法使用对象提供的键访问对象中的值?

转载 作者:行者123 更新时间:2023-11-28 05:41:04 26 4
gpt4 key购买 nike

我正在尝试使用 Google Apps 脚本中的 Javascript 从对象生成数组,以便可以将该数组写入工作表。我有一个对象 campers,其中包含由唯一编号标识的每个人的对象。当我尝试访问该人的记录时,我得到了未定义的信息。函数如下:

function processData(data){  // Returning from client with full object - parse and write to spreadsheet
Logger.log(data)
var campers = data; // All camper data and headers
var headers = campers.headers; // Array of ordered questions
var nHeaders = headers.length;
var array = []; // Array to build - each row is one camper

for (uid in campers){ // All objects in campers object
if (uid!='headers'){ // want campers only
Logger.log(uid);
Logger.log(campers[uid]);
var row = [];
for (var j=0; j<nHeaders; j++){ // Go through all questions
if(campers[uid][headers[j]]) row.push(campers[uid][headers[j]]); // Add that value to the array if it exists
else row.push(null);
}
array.push(row);
}
}
var final = ss.getSheetByName('ParseUpload');
final.getRange("A1").offset(0,0,array.length, nHeaders).setValues(array);
return;
}

当我运行它时,我得到以下日志:

[16-08-12 02:07:31:240 PDT] {headers=[Card Number, Unique ID, Last Name, First Name, Email, Status], 123456={Status=Non-Attend, Unique ID=123456, Email=test3@test.edu, First Name=John, Card Number=456789012, Last Name=Doe}, 987654={Status=Attend, Unique ID=987654, Email=test2@test.edu, First Name=Jane, Card Number=5, Last Name=Doe}, 1234567={Status=Attend, Unique ID=1234567, Email=test1@test.edu, First Name=John, Card Number=123456789, Last Name=Smith}}
[16-08-12 02:07:31:298 PDT] 123456
[16-08-12 02:07:31:299 PDT] undefined
[16-08-12 02:07:31:300 PDT] 987654
[16-08-12 02:07:31:301 PDT] undefined
[16-08-12 02:07:31:301 PDT] 1234567
[16-08-12 02:07:31:302 PDT] undefined

为什么 uid 的值在 campers 中肯定是一个键,但对于 campers[uid] 返回 undefined ?我搜索了类似的问题,并找到了如何使用括号表示法访问对象的引用,但没有任何内容可以在已定义的内容上未定义(我认为)。请告诉我这里明显遗漏了一些东西,或者如果这确实是重复的,请引导我到另一个问题。

这是数据的结构:

{
"headers": ["Card Number", "Unique ID", "Last Name", "First Name", "Email", "Status"],
"123456": {
"First Name": "John",
"Unique ID": 123456,
"Email": "test3#test.edu",
"Card Number": 456789012,
"Last Name": "Doe",
"Status": "Non-Attend"
},
"987654": {
"First Name": "Jane",
"Unique ID": 987654,
"Email": "test2@test.edu",
"Card Number": 5,
"Last Name": "Doe",
"Status": "Attend"
},
"1234567": {
"First Name": "John",
"Unique ID": 1234567,
"Email": "test1@test.edu",
"Card Number": 123456789,
"Last Name": "Smith",
"Status": "Attend"
}
}

编辑如果我使用上述结构直接在函数中定义campers,它就可以正常工作:

function processData(){ 
var campers = {
"headers": ["Card Number", "Unique ID", "Last Name", "First Name", "Email", "Status"],
"123456": {
"First Name": "John",
"Unique ID": 123456,
"Email": "test3@test.edu",
"Card Number": 456789012,
"Last Name": "Doe",
"Status": "Non-Attend"
},
"987654": {
"First Name": "Jane",
"Unique ID": 987654,
"Email": "test2@test.edu",
"Card Number": 5,
"Last Name": "Doe",
"Status": "Attend"
},
"1234567": {
"First Name": "John",
"Unique ID": 1234567,
"Email": "test1@test.edu",
"Card Number": 123456789,
"Last Name": "Smith",
"Status": "Attend"
}
}
var headers = campers.headers; // Array of ordered questions
var nHeaders = headers.length;
var array = []; // Array to build - each row is one camper

for (uid in campers){ // All objects in campers object
if (uid!='headers'){ // want campers only
Logger.log(uid);
Logger.log(campers[uid]);
var row = [];
for (var j=0; j<nHeaders; j++){ // Go through all questions
if(campers[uid][headers[j]]) row.push(campers[uid][headers[j]]); // Add that value to the array if it exists
else row.push(null);
}
array.push(row);
}

}
Logger.log(array);
//var final = ss.getSheetByName('ParseUpload');
//final.getRange("A1").offset(0,0,array.length, nHeaders).setValues(array);
return;
}

这有帮助吗?也许有人可以明白为什么本地定义的 campers 可以工作,但当作为 data 传入时却不起作用。

编辑2我尝试了一些额外的日志记录,但没有更改函数的其余部分:

var keys = Object.keys(campers);
Logger.log(keys)
for (var uid in campers){ // All objects in campers object

if (uid!='headers'){ // want campers only
Logger.log(uid);
Logger.log(keys.indexOf(uid));
Logger.log(campers[uid]);
Logger.log(campers[keys[0]]);
Logger.log(campers[Object.keys(campers)[0]]);
Logger.log(campers[123456]);
Logger.log(campers['123456']);
var row = [];
for (var j=0; j<nHeaders; j++){ // Go through all questions
if(campers[uid][headers[j]]) row.push(campers[uid][headers[j]]); // Add that value to the array if it exists
else row.push(null);
}
array.push(row);
}
}

这是生成的日志:

[16-08-12 07:37:57:069 EDT] [123456, 987654, 1234567, headers]
[16-08-12 07:37:57:070 EDT] 123456
[16-08-12 07:37:57:072 EDT] 0.0
[16-08-12 07:37:57:073 EDT] undefined
[16-08-12 07:37:57:074 EDT] undefined
[16-08-12 07:37:57:075 EDT] undefined
[16-08-12 07:37:57:075 EDT] undefined
[16-08-12 07:37:57:076 EDT] undefined

因此,在键列表中找到了 uid,但这些值似乎无法访问。呃!

最佳答案

我仍然不知道是什么导致了这个问题,但我找到了解决办法。感谢 TJ.Crowder 的评论要求我显示 JSON.stringify(data) 结果并想知道该对象是否不是标准 JavaScript 对象,我在输入函数时尝试了以下操作:

var data2 = JSON.stringify(data);
var campers = JSON.parse(data2);

出于某种原因,将对象转换为 JSON 字符串然后立即返回对象是可行的。

关于javascript - 为什么我无法使用对象提供的键访问对象中的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38914873/

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