【问题标题】:Problem with iterating Nested Array output in n8n javascript在 n8n javascript 中迭代嵌套数组输出的问题
【发布时间】:2021-07-06 18:41:46
【问题描述】:

我有一个对 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 表格时,它看起来像这样(每个结果):

Screenshot of table

如何让嵌套数组正确列出? (注意空的“指标”结果。

Javascript(是 n8n 系统使用的)不是我的包,所以我自己在这里进行 javascript 故障排除,并寻求一些帮助来解决最后一个障碍。

【问题讨论】:

  • 您的“indicators”结构元素没有“name”属性,因此“data.name”将未定义。你想要那个值数组吗?
  • 是的,大部分是values数组
  • 啊,我现在明白你的意思了。通过将其命名为 data.values,我可以将它们显示在那里。但是,如果我还想解析其余部分,是否也可以这样做,只需像 .replace 函数中一样用 \n 重复每个结果?
  • 看看对象扫描。它是为这个用例制作的(免责声明我是作者)

标签: javascript arrays nested


【解决方案1】:

这将为您提供所有“指标”中的所有“值”,并加入一个列表。因此,如果数组中有 2 个指标,每个指标有 4 个值,您将获得一个包含 8 个“值”的列表。可能不理想,可能有重复。

还可以查看Array.join - 你会看到我在这里使用了两次,它是 .toString.replace 的更好替代品。

json[key] = element[key].map(data => data.values.join("\n")).join("\n");

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-01
    相关资源
    最近更新 更多