免责声明:据我所知,所有出现的名字都是虚构的,并不代表真实个人的名字。任何这样的发生都纯属巧合。
我正在调用一个外部api并获得结果。请参阅响应的片段。但是,当试图从数组中的attributes对象中提取name值时,我遇到了一个问题。
"included": [
13 items
0: {
5 items
"type": "Person"
"id": "96840658"
"attributes": {
28 items
"accounting_administrator":False
"anniversary":NULL
"avatar": "https://avatars.planningcenteronline.com/uploads/initials/MS.png"
"birthdate":NULL
"can_create_forms":False
"child":False
"created_at": "2021-08-22T18:03:28Z"
"demographic_avatar_url": "https://avatars.planningcenteronline.com/uploads/initials/MS.png"
"directory_status": "no_access"
"first_name": "Kloogy"
"gender":NULL
"given_name":NULL
"grade":NULL
"graduation_year":NULL
"inactivated_at":NULL
"last_name": "Sexton"
"medical_notes":NULL
"membership":NULL
"middle_name":NULL
"name": "Kloogy Sexton"
"nickname":NULL
"passed_background_check":False
"people_permissions":NULL
"remote_id":NULL
"school_type":NULL
"site_administrator":False
"status": "active"
"updated_at": "2021-08-22T18:03:28Z"
}该数组在列表中有13个对象。我正在循环遍历13个对象,用下面的应用程序脚本代码提取名称,并将其推送到一个名为“候选人”的数组中。我希望将名称作为一个列表放在一个数组中。然而,当我查看日志时,我得到的是13个数组,每个数组以另一个名称递增,直到最后一个数组中的所有13个名称都被输出为止。
//Parse the JSON reply
var data = response.getContentText();
var results = JSON.parse(data);
var people = results['included'];
Logger.log(people)
var numberofpeople = results['data']['attributes']['total_people'];
Logger.log(numberofpeople)
var candidates = []
numRows = results['data']['attributes']['total_people']
//Logger.log(numRows)
for (var i = 0; i < numberofpeople; i++) {
candidates.push(i,
people[i]["attributes"]["name"],
)
Logger.log(candidates)
}原木样本。
[0.0, Kloogy Sexton]
[0.0, Kloogy Sexton, 1.0, Valentine Dudley]
[0.0, Kloogy Sexton, 1.0, Valentine Dudley, 2.0, Jackson Underwoof]
[0.0, Kloogy Sexton, 1.0, Valentine Dudley, 2.0, Jackson Underwoof, 3.0, Akeem Smiley]
[0.0, Kloogy Sexton, 1.0, Valentine Dudley, 2.0, Jackson Underwoof, 3.0, Akeem Smiley, 4.0, Brianna Booleee]任何关于我在循环中出错的提示都将不胜感激。
发布于 2021-08-24 11:30:15
在这种情况下,我喜欢使用map:
const candidateNames = results.included.map(person => person.attributes.name)尽管如此,您的代码不能工作的原因是因为您使用的是push(i, name)。JavaScript push总是在后面添加。它还支持任意数量的参数到push,这些参数是按顺序添加的。结果是[0, <name 0>, 1, <name 1>, ...]。
参考文献
发布于 2021-08-23 20:49:18
也许你需要这个:
candidates.push([ i, people[i]["attributes"]["name"] ]);或者这个:
candidates.push(i + ", " + people[i]["attributes"]["name"]);或者:
candidates.push(people[i]["attributes"]["name"]);这取决于您所说的“作为一个数组中的列表”的确切含义。
var results = {"included": [
{ "id": "96840658", "attributes": { "name": "Kloogy Sexton" } },
{ "id": "96840659", "attributes": { "name": "John Smit" } },
{ "id": "96840660", "attributes": { "name": "Nobody" } },
]};
var people = results['included'];
var numberofpeople = people.length;
var candidates = [];
for (var i = 0; i < numberofpeople; i++) {
candidates.push([i, people[i]["attributes"]["name"] ] );
}
console.log(candidates)
var candidates = [];
for (var i = 0; i < numberofpeople; i++) {
candidates.push(i + ", " + people[i]["attributes"]["name"] );
}
console.log(candidates)
var candidates = [];
for (var i = 0; i < numberofpeople; i++) {
candidates.push(people[i]["attributes"]["name"]);
}
console.log(candidates)
https://stackoverflow.com/questions/68898794
复制相似问题