Function with loop array

Hi there,
I am trying to do a loop using an array, but I don’t know how to do it.
Can someone give me some tips?

{
  "name": "My workflow",
  "nodes": [
    {
      "parameters": {},
      "name": "Start",
      "type": "n8n-nodes-base.start",
      "typeVersion": 1,
      "position": [
        260,
        260
      ]
    },
    {
      "parameters": {
        "functionCode": "const ids = ['wrongNumber','00000000000000', '00000000000191', '00000112870120', '00000776000101']\n\nfor (const zid of ids) {\n    console.log(zid);\n}\n\n"
      },
      "name": "Function",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        500,
        260
      ]
    },
    {
      "parameters": {
        "conditions": {
          "string": [
            {
              "operation": "regex",
              "value2": "/^([^0-9]*)$/"
            }
          ]
        }
      },
      "name": "IF is Number",
      "type": "n8n-nodes-base.if",
      "typeVersion": 1,
      "position": [
        660,
        140
      ]
    },
    {
      "parameters": {},
      "name": "IF == 00000112870120",
      "type": "n8n-nodes-base.if",
      "typeVersion": 1,
      "position": [
        900,
        120
      ]
    },
    {
      "parameters": {},
      "name": "NoOp",
      "type": "n8n-nodes-base.noOp",
      "typeVersion": 1,
      "position": [
        1120,
        100
      ]
    }
  ],
  "connections": {
    "Start": {
      "main": [
        [
          {
            "node": "Function",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "IF is Number": {
      "main": [
        [
          {
            "node": "IF == 00000112870120",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Function",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "IF == 00000112870120": {
      "main": [
        [
          {
            "node": "NoOp",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Function",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "settings": {}
}

As long as the function node returns the array in the right format, n8n will automatically iterate over all inputs. Check the example below.

{
  "nodes": [
    {
      "parameters": {},
      "name": "Start",
      "type": "n8n-nodes-base.start",
      "typeVersion": 1,
      "position": [
        -690,
        260
      ]
    },
    {
      "parameters": {
        "functionCode": "const ids = ['wrongNumber','00000000000000', '00000000000191', '00000112870120', '00000776000101']\n\nconsole.log(ids.map((id) => ({ json: id })))\n\nreturn ids.map((id) => ({ json:{ id } }))"
      },
      "name": "Function",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        -440,
        260
      ]
    },
    {
      "parameters": {
        "conditions": {
          "string": [
            {
              "value1": "={{ $node[\"if it's a number\"].json.id }}",
              "value2": "00000112870120"
            }
          ]
        }
      },
      "name": "IF == 00000112870120",
      "type": "n8n-nodes-base.if",
      "typeVersion": 1,
      "position": [
        100,
        90
      ]
    },
    {
      "parameters": {},
      "name": "NoOp",
      "type": "n8n-nodes-base.noOp",
      "typeVersion": 1,
      "position": [
        470,
        40
      ]
    },
    {
      "parameters": {},
      "name": "NoOp1",
      "type": "n8n-nodes-base.noOp",
      "typeVersion": 1,
      "position": [
        90,
        380
      ]
    },
    {
      "parameters": {
        "conditions": {
          "string": [
            {
              "value1": "={{ $node[\"Function\"].json[\"id\"] }}",
              "operation": "regex",
              "value2": "/[0-9]/g"
            }
          ]
        }
      },
      "name": "if it's a number",
      "type": "n8n-nodes-base.if",
      "typeVersion": 1,
      "position": [
        -180,
        260
      ]
    }
  ],
  "connections": {
    "Start": {
      "main": [
        [
          {
            "node": "Function",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Function": {
      "main": [
        [
          {
            "node": "if it's a number",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "IF == 00000112870120": {
      "main": [
        [
          {
            "node": "NoOp",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "if it's a number": {
      "main": [
        [
          {
            "node": "IF == 00000112870120",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "NoOp1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

Hi @RicardoE105, first of all, thank you for your prompt answer.
Your solution is good, however, I’d like to do something like for each position on the array a loop, for instance, if the array has 5 positions, then the loop should occur 5 times and thus perform the validations of both IFs.

Hey @tuliovargas,

Doesn’t the example that Ricardo shared, solve the issue? You don’t have to create a loop. Almost all the nodes in n8n iterate over all incoming items. If you execute the example workflow, you will observe that both the IF nodes process all the incoming data.
You can learn more about looping in n8n in the docs here: Looping in n8n | Docs

If I missed something, or if anything is not clear, please let us know :slight_smile:

Hi @harshil1712 and @RicardoE105 ,
Can you see it:

  • If validation is not working, it should be looping only the array’s positions;
  • How to for each execution just show the position “i” of the array?

{
  "nodes": [
    {
      "parameters": {},
      "name": "Start",
      "type": "n8n-nodes-base.start",
      "typeVersion": 1,
      "position": [
        -880,
        780
      ]
    },
    {
      "parameters": {
        "functionCode": "const ids = ['04104020', '05405250', '12770-000', '05406100', '05406200']\n\nconsole.log(ids.map((id) => ({ json: id })))\n\nreturn ids.map((postcode) => ({ json:{ postcode } }))"
      },
      "name": "Function2",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        -570,
        780
      ]
    },
    {
      "parameters": {
        "conditions": {
          "string": [],
          "number": [
            {
              "value1": "={{$node[\"Function2\"].json[\"postcode\"].length}}",
              "operation": "largerEqual",
              "value2": 5
            }
          ]
        }
      },
      "name": "if it's a number1",
      "type": "n8n-nodes-base.if",
      "typeVersion": 1,
      "position": [
        -310,
        780
      ]
    },
    {
      "parameters": {
        "url": "=https://ws.apicep.com/cep/{{$node[\"Function2\"].json[\"postcode\"]}}.json",
        "options": {}
      },
      "name": "HTTP Request",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 1,
      "position": [
        -100,
        600
      ],
      "executeOnce": false
    },
    {
      "parameters": {},
      "name": "END",
      "type": "n8n-nodes-base.noOp",
      "typeVersion": 1,
      "position": [
        -100,
        890
      ]
    }
  ],
  "connections": {
    "Start": {
      "main": [
        [
          {
            "node": "Function2",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Function2": {
      "main": [
        [
          {
            "node": "if it's a number1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "if it's a number1": {
      "main": [
        [
          {
            "node": "HTTP Request",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "END",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "HTTP Request": {
      "main": [
        [
          {
            "node": "if it's a number1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

Hey @tuliovargas,

To make sure that I understood it correctly, you want to use the condition on the item’s position and not the item itself, right?

For example, if the array is [1, 4, 53, 34], you want to check for the position (eg. 2) and not the element itself. Is that right?