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

Hello, I am able to merge “item_name” ( using script provided in function node ) based on “name”, however, can i add in a comma “,” in between “balance” in the output?

merge Original json

to following. However, “item_name” is stick to one anther.

const results = []
const names = {}

for (const item of items) {
  if (names[item.json.name] !== undefined) {
      names[item.json.name] = (names[item.json.name]).concat(item.json['item_name'])
  } else {
    names[item.json.name] = item.json['item_name']
  }
}

for (const name of Object.keys(names)) {
  results.push({
    json: {
      name,
      balance: names[name]
    }
  })
}

return results


because your data in objects , not arrays.
First you must convert your data to array […]

1 Like