While loop inside expression?

Is there a way to use a while loop inside an expression? I’m trying to fill in the CC: field in Send Mail node whith a list of mails:
{{$json[“properties”][“Assigned to”][“people”][0][“person”][“email”]}};

{{$json[“properties”][“Assigned to”][“people”][1][“person”][“email”]}};

{{$json[“properties”][“Assigned to”][“people”][2][“person”][“email”]}}

would like to use while loop: i=0, while ({{$json[“properties”][“Assigned to”][“people”][i][“person”][“email”]}}!="") … add the {{$json[“properties”][“Assigned to”][“people”][i][“person”][“email”]}} and “;” to the field
Can someone please help me with the syntax?

Hi @ajcairns, the CC field of the Send Mail node accepts multiple email addresses separated with a comma. Instead of using an expression (which doesn’t support multiple lines) you could loop through the email field of your example data using a Function Item node (which runs once for each input item).

Instead of looping, it might, however, be easier to use JavaScript’s map() to get the email addresses only followed by join() to generate the required string.

Example Workflow
{
  "nodes": [
    {
      "parameters": {
        "functionCode": "item.email_cc = item[\"Assigned to\"].people.map(e => e.person.email).join(',');\n\nreturn item;"
      },
      "name": "Create CC String",
      "type": "n8n-nodes-base.functionItem",
      "typeVersion": 1,
      "position": [
        700,
        300
      ]
    },
    {
      "parameters": {},
      "name": "Start",
      "type": "n8n-nodes-base.start",
      "typeVersion": 1,
      "position": [
        240,
        300
      ]
    },
    {
      "parameters": {
        "functionCode": "return [{\n  json: {\n    'Assigned to': {\n      people: [{\n        person: {\n          email: '[email protected]'\n        }\n      }, {\n        person: {\n          email: '[email protected]'\n        }\n      }]\n    }\n  }\n}]"
      },
      "name": "Set Example Data",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        480,
        300
      ]
    },
    {
      "parameters": {
        "functionCode": "item.email_cc = item[\"Assigned to\"].people.map(e => e.person.email).join(',');\n\nreturn item;"
      },
      "name": "Create CC String",
      "type": "n8n-nodes-base.functionItem",
      "typeVersion": 1,
      "position": [
        700,
        300
      ]
    },
    {
      "parameters": {
        "ccEmail": "={{$json[\"email_cc\"]}}",
        "options": {}
      },
      "name": "Send Email",
      "type": "n8n-nodes-base.emailSend",
      "typeVersion": 1,
      "position": [
        920,
        300
      ]
    }
  ],
  "connections": {
    "Create CC String": {
      "main": [
        [
          {
            "node": "Send Email",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Start": {
      "main": [
        [
          {
            "node": "Set Example Data",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Set Example Data": {
      "main": [
        [
          {
            "node": "Create CC String",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

Hope this works for you :slight_smile:

Let me know if you’re having trouble!

Thank you, @MutedJam. :+1::smiley:Managed to get it to work with function item, and while with test on the number of items (empty string throws up an error, I suppose because it goes beyond end of the array).
Will also try and check out the map/join method.

1 Like