How to join elements in array by Merge key / Combine arrays by key by Function item

Hello community, I need your help. This is what the incoming data looks like.

How to achieve the result so that if the key matches the values are appended

My WorkFlow:

{ "name": "Append data in array", "nodes": [ { "parameters": {}, "name": "Start", "type": "n8n-nodes-base.start", "typeVersion": 1, "position": [ 250, 300 ] }, { "parameters": { "functionCode": "return [\n {\n json: {\n \"name\": \"Maxim\",\n \"data\": ['good','man']\n }\n },\n {\n json: {\n \"name\": \"Liza\",\n \"data\": ['good','woman']\n }\n },\n {\n json: {\n \"name\": \"Cat\",\n \"data\": ['pretty','cat']\n }\n },\n {\n json: {\n \"name\": \"Maxim\",\n \"data\": ['student']\n }\n },\n {\n json: {\n \"name\": \"Dog\",\n \"data\": ['safety','dog']\n }\n }\n]" }, "name": "incomingData", "type": "n8n-nodes-base.function", "typeVersion": 1, "position": [ 510, 290 ] }, { "parameters": { "functionCode": "return [\n {\n json: {\n \"name\": \"Maxim\",\n \"data\": ['good','man','student']\n }\n },\n {\n json: {\n \"name\": \"Liza\",\n \"data\": ['good','woman']\n }\n },\n {\n json: {\n \"name\": \"Cat\",\n \"data\": ['pretty','cat']\n }\n },\n {\n json: {\n \"name\": \"Dog\",\n \"data\": ['safety','dog']\n }\n }\n]" }, "name": "ResultDataExample", "type": "n8n-nodes-base.function", "typeVersion": 1, "position": [ 730, 290 ] } ], "connections": { "incomingData": { "main": [ [ { "node": "ResultDataExample", "type": "main", "index": 0 } ] ] }, "Start": { "main": [ [ { "node": "incomingData", "type": "main", "index": 0 } ] ] } }, "active": false, "settings": {}, "id": "16" }

Check the example below:

{
  "nodes": [
    {
      "parameters": {},
      "name": "Start",
      "type": "n8n-nodes-base.start",
      "typeVersion": 1,
      "position": [
        140,
        300
      ]
    },
    {
      "parameters": {
        "functionCode": "return [\n  {\n    json: {\n      name: 'Maxim',\n      data: [\"good\", \"man\"],\n    }\n  },\n    {\n    json: {\n      name: 'Liza',\n      data: [\"good\", \"woman\"],\n    }\n  },\n      {\n    json: {\n      name: 'Cat',\n      data: [\"pretty\", \"cat\"],\n    }\n  },\n        {\n    json: {\n      name: 'Maxim',\n      data: [\"student\"],\n    }\n  },\n          {\n    json: {\n      name: 'Dog',\n      data: [\"safety\", \"dog\"],\n    }\n  }\n]\n\n"
      },
      "name": "Mockup data",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        380,
        300
      ]
    },
    {
      "parameters": {
        "functionCode": "const results = []\nconst names = {}\n\nfor (const item of items) {\n  if (names[item.json.name] !== undefined) {\n      names[item.json.name] = (names[item.json.name]).concat(item.json.data)\n  } else {\n    names[item.json.name] = item.json.data\n  }\n}\n\nfor (const name of Object.keys(names)) {\n  results.push({\n    json: {\n      name,\n      data: names[name]\n    }\n  })\n}\n\nreturn results\n\n\n\n"
      },
      "name": "Function",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        620,
        300
      ]
    }
  ],
  "connections": {
    "Start": {
      "main": [
        [
          {
            "node": "Mockup data",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Mockup data": {
      "main": [
        [
          {
            "node": "Function",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}
1 Like