Javascript 通过Node.JSON输出从Asana任务API提取特定项目ID
使用,我们可以查看任务所属项目的列表,以及这些项目的GID和注释(描述文本) 预期结果 这里的全部目标是获取项目的GID,该项目的#websiteprojecttemplate在其Notes值内。我们需要找到该项目的GID,然后输出它,以便稍后在Zapier操作中使用该GID Asana任务API 使用这个API URL,我们可以看到返回项目数据(包括GID和Notes)的API的输出。我相信这是JSON<代码>https://app.asana.com/api/1.0/tasks/{id}?opt_fields=projects.notes 例如:Javascript 通过Node.JSON输出从Asana任务API提取特定项目ID,javascript,node.js,json,asana,asana-api,Javascript,Node.js,Json,Asana,Asana Api,使用,我们可以查看任务所属项目的列表,以及这些项目的GID和注释(描述文本) 预期结果 这里的全部目标是获取项目的GID,该项目的#websiteprojecttemplate在其Notes值内。我们需要找到该项目的GID,然后输出它,以便稍后在Zapier操作中使用该GID Asana任务API 使用这个API URL,我们可以看到返回项目数据(包括GID和Notes)的API的输出。我相信这是JSONhttps://app.asana.com/api/1.0/tasks/{id}?opt_f
https://app.asana.com/api/1.0/tasks/1799885428032109?opt_fields=projects.notes
显示如下:
现行代码 理想情况下,Node.js Javascript代码能够迭代/搜索/查找带有
#websiteprojecttemplate
的代码,然后输出匹配的GID。在这种情况下,它应该是:1199916857565229
这是我到目前为止的JS代码:
const res = await fetch('https://app.asana.com/api/1.0/tasks/' + inputData.uniqueID + '?opt_fields=projects.notes', {
headers: {
'Authorization': 'Bearer 0/899removedforsecurity24564s'
}
});
const body = await res.json();
const projects = body.data;
output = { id: projects };
"id" : {
"gid" : "1199885428032109",
"projects" : [ {
"gid" : "810573962916457",
"notes" : "CRM project to create custom fields for the CRM task"
}, {
"gid" : "881219806802782",
"notes" : "Helps keep PMs aware of what stage of progress the website projects are at; as well as how many projects each PM has (via saved Asana searches)."
}, {
"gid" : "1129624391492919",
"notes" : "Tracks the stage and progress of converting a lead to a client."
}, {
"gid" : "1140671985497468",
"notes" : "Additional CRM project to create more custom fields for the CRM task"
}, {
"gid" : "1199916857565229",
"notes" : "Created from the #websiteprojecttemplate."
} ]
}
输出如下内容:
const res = await fetch('https://app.asana.com/api/1.0/tasks/' + inputData.uniqueID + '?opt_fields=projects.notes', {
headers: {
'Authorization': 'Bearer 0/899removedforsecurity24564s'
}
});
const body = await res.json();
const projects = body.data;
output = { id: projects };
"id" : {
"gid" : "1199885428032109",
"projects" : [ {
"gid" : "810573962916457",
"notes" : "CRM project to create custom fields for the CRM task"
}, {
"gid" : "881219806802782",
"notes" : "Helps keep PMs aware of what stage of progress the website projects are at; as well as how many projects each PM has (via saved Asana searches)."
}, {
"gid" : "1129624391492919",
"notes" : "Tracks the stage and progress of converting a lead to a client."
}, {
"gid" : "1140671985497468",
"notes" : "Additional CRM project to create more custom fields for the CRM task"
}, {
"gid" : "1199916857565229",
"notes" : "Created from the #websiteprojecttemplate."
} ]
}
但我需要它输出的不是整个身体的数据,而是在其注释值中包含#websiteprojecttemplate
的gid
值
TL;博士
我们如何更新当前代码,以便它可以遍历gid
,并在notes
中找到具有#websiteprojecttemplate
的代码,并输出它的gid
编号?我想你的问题归结为如何在数组中找到元素
const project = projects.projects.find(p => p.notes.includes("#websiteprojecttemplate"));
if (project) {
console.log(project.gid);
} else {
console.log("no matching project");
}
这似乎很有效。出于好奇,为什么使用p
而不是project
?只是短一点吗?如果API输出中的另一个键以p开头,该怎么办?这部分p=>p.notes.includes(“#websiteprojecttemplate”)
是一个函数,它接受参数p并返回真/假值。参数名可以是任何名称,并且与.find操作的数组中的属性无关。是的,它是的,它是短的:)