Create Multiple Items with map function

Hello,

I’m still learning and tried this now for a couple of hours. I want to crate multiple items from a text (for example from a HTTP request) that i splitted to an array with the split function.

I tried it with the map function that i found here on the forum, but im getting duplicate entries in the item rows. This is what I have so far.

{
  "nodes": [
    {
      "parameters": {
        "values": {
          "string": [
            {
              "name": "originaldata",
              "value": "spotify.com/deezer.com/tidal.com/applemusic.com"
            }
          ]
        },
        "options": {}
      },
      "name": "Original Data",
      "type": "n8n-nodes-base.set",
      "typeVersion": 1,
      "position": [
        430,
        310
      ]
    },
    {
      "parameters": {
        "functionCode": "var arraylength = items[0].json.splitdata.length; \n\nfor (i=0; i<=arraylength; i++) { \n\nreturn items[i].json.splitdata.map(item => {\n  return {\n    json: { url: items[i].json.splitdata[i] }\n  }\n});\n\n}\n\n\n"
      },
      "name": "Create Multiple Items",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        810,
        310
      ]
    },
    {
      "parameters": {
        "functionCode": "var original = items[0].json.originaldata;\n\nitems[0].json.splitdata = original.split('/');\n\nreturn items;\n"
      },
      "name": "Split Data",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        630,
        310
      ]
    }
  ],
  "connections": {
    "Original Data": {
      "main": [
        [
          {
            "node": "Split Data",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Split Data": {
      "main": [
        [
          {
            "node": "Create Multiple Items",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

As you see it’s showing “spotify.com” 4 times, but the result should be like this:

{
  "nodes": [
    {
      "parameters": {
        "functionCode": "return [\n  {\n    json: {\n       url: 'spotify.com'\n    }\n  },\n  {\n    json: {\n      url: 'deezer.com'\n    }\n  },\n  {\n    json: {\n      url: 'tidal.com'\n    }\n  },\n    {\n    json: {\n      url: 'applemusic.com'\n    }\n  }\n]\n\n"
      },
      "name": "Result Mock Data",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        380,
        630
      ]
    }
  ],
  "connections": {}
}

Maybe something small that I’m missing here. Thanks in advance for your help.

Hey @ottic!

You can learn about modifying the data structure on our documentation.

Let me know if you still need help :slightly_smiling_face:

Try this:

return items[0].json.originaldata.split("/").map(url => ({ json: { url } }));

Awesome. That’s working perfectly. Thank you so much.