我有一个对SaaS服务的API查询,它输出了大量的结果,我在n8n function node (基于javascript)中迭代了这些结果。然而,一些结果是深度嵌套的数组,我正在努力使输出正确工作。对于单级嵌套数组,它工作得很好。
我有一个巨大的单一JSON输入,就像这样(显示结构的许多结果之一):
{
"success": true,
"articles": [
{
"guid": "aaaa1234",
"title": "some interesting text here",
"summary": "some interesting text here.",
"type": "public",
"publishedDate": "2021-07-05T07:00:00.000+00:00",
"link": "https://some_interesting_text_here",
"categories": [],
"tags": [
"aab",
"bb",
"cc",
"dd",
"ee",
"ff",
"gg",
"hh"
],
"indicators": [
{
"type": "some interesting text here",
"count": 4,
"values": [
"some interesting text here",
"some interesting text here",
"some interesting text here",
"some interesting text here"
],
"source": "public"
}
]
},迭代结果的n8n函数节点如下所示(将结果拆分成单独的结果):
const results = []
for (const item of items) {
results.push.apply(results, item.json.articles)
}
return results.map(element => {
const json = {};
for (const key of Object.keys(element)) {
if (key === 'tags') {
json[key] = element[key].toString().replace(/,/g, '\n');
continue;
}
if (key === 'indicators') {
json[key] = element[key].map(data => data.name).toString().replace(/,/g, '\n');
continue;
}
// All others that are not specifically set will fall back to the default logic
json[key] = typeof element[key] === 'object' ? JSON.stringify(element[key]) : element[key];
}
return { json };
})输出如下所示,
{
"guid": "aaaa1234",
"title": "some interesting text here",
"summary": "some interesting text here",
"type": "public",
"publishedDate": "2021-07-05T07:00:00.000+00:00",
"link": "https://some_interesting_text_here",
"categories": "[]",
"tags": "aa bb cc dd ee ff gg hh",
"indicators": " "
},然后导出到Google工作表时,它看起来像这样(每个结果):
如何才能正确列出嵌套数组?(请注意空的“指示器”结果。
javascript (是n8n系统使用的)不是我的拿手好戏,所以我在这里独自解决我的javascript问题,并寻找一些帮助来解决最后一个障碍。
发布于 2021-07-07 05:06:12
这将为您提供所有“指标”中的所有“值”,并加入到一个列表中。因此,如果数组中有2个指示器,每个指示器中有4个值,那么您将得到一个包含8个“值”的列表。可能不理想,可能会有重复。
另外看看Array.join -你会看到我在这里用了两次,它是.toString.replace的一个更好的替代品。
json[key] = element[key].map(data => data.values.join("\n")).join("\n");https://stackoverflow.com/questions/68275892
复制相似问题