Function Node: Turn a list of items into an array of items using push

Hi N8N’rs!

I have a Function node challenge.

I have an list of items, with fields that has no JSON parent.

I would like to put each of the items under a parent item “cells”

So current simple structure is:

{
“key” : “value”,
“key2” : “value”
},
{
“key” : “value”,
“key2” : “value”
},
etc…

Want to make it so the items are an array under the key “cells”:

{
“cells” : [
{
“key” : “value”,
“key2” : “value”
},
{
“key” : “value”,
“key2” : “value”
},
]}

I’m having trouble working out the function node logic to nail this. I know it’s probably fairly simple…

Thanks for any help,

The function node should look something like:

const data = items.map((item) => item.json);

return [
  {
    json: {
      cells: data
    }
  }
]
Example workflow
{
  "nodes": [
    {
      "parameters": {},
      "name": "Start",
      "type": "n8n-nodes-base.start",
      "typeVersion": 1,
      "position": [
        -180,
        80
      ]
    },
    {
      "parameters": {
        "functionCode": "return [\n  {\n    json: {\n    \n        \n\"key\" : \"value\",\n\"key2\" : \"value\"\n\n    }\n  },\n    {\n    json: {\n    \n        \n\"key\" : \"value\",\n\"key2\" : \"value\"\n\n    }\n  }\n]"
      },
      "name": "Function",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        80,
        80
      ]
    },
    {
      "parameters": {
        "functionCode": "\nconst data = items.map((item) => item.json);\n\nreturn [\n  {\n    json: {\n      cells: data\n    }\n  }\n]\n"
      },
      "name": "Function1",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        310,
        80
      ]
    }
  ],
  "connections": {
    "Start": {
      "main": [
        [
          {
            "node": "Function",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Function": {
      "main": [
        [
          {
            "node": "Function1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}
1 Like