How to generate uuid at once for a large array of rows?

Hi
How to generate uuid at once for a large array of rows?
Now I have implemented a function for only one line?
I am using a script like this in the function node:

You can do the following:

const uuid = require('uuid');

items.forEach(item => {
  item.json.uuid = uuid.v4();
});

return items;

Here a full example:

{
  "nodes": [
    {
      "parameters": {
        "functionCode": "const newItems = [];\n\nfor (let i=0;i<10;i++) {\n  newItems.push({ json: {i} });\n}\n\nreturn newItems;"
      },
      "name": "Mock Input Data",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        450,
        300
      ]
    },
    {
      "parameters": {
        "functionCode": "const uuid = require('uuid');\n\nitems.forEach(item => {\n  item.json.uuid = uuid.v4();\n});\n\nreturn items;"
      },
      "name": "Function",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        650,
        300
      ]
    }
  ],
  "connections": {
    "Mock Input Data": {
      "main": [
        [
          {
            "node": "Function",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}
1 Like

Thanks
Its work