How could I create object from array?

Hi, I use function node to create this object from this array:

My array:

[
"text 1",
"text 2",
"text 3"
]

My object I want to return:

return [
  {
    json: {
      message: "text 1"
    } 
  },
  {
    json: {
      message: "text 2"
    }
  },
  {
    json: {
      message: "text 3"
    }
  }
]

Assuming that the data is saved in the property “data” you can do the following in a Function-Node:

return items[0].json.data.map(message => {
  return {
    json: {
      message
    },
  }
});

Here an example workflow:

{
  "nodes": [
    {
      "parameters": {
        "functionCode": "items[0].json.data = [\n\"text 1\",\n\"text 2\",\n\"text 3\"\n];\nreturn items;"
      },
      "name": "Mock Data",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        469,
        358
      ]
    },
    {
      "parameters": {
        "functionCode": "return items[0].json.data.map(message => {\n  return {\n    json: {\n      message\n    },\n  }\n})"
      },
      "name": "Split",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        669,
        358
      ]
    }
  ],
  "connections": {
    "Mock Data": {
      "main": [
        [
          {
            "node": "Split",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}
2 Likes

Yeah, map is solution
Thank you very much