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

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

Dear MutedJam,

After searching a lot, I get to this feed. I need a similar solution to map different fields based on a condition. Following this example, imagine that I have three different mails and another property called urgent set only for two of them.

Now I need to create the fields mapping only those registers that match one (or two conditions).

I tried to doit with a function using while buy it’s not working.

I’m trying something like this but is not working:

for (item of items) {
  item.json.myNewField = "";
  i = 0
  while (i < 3) {
    if (item.json.fields["Alevin Invierno"][i] === true) {
      item.json.myNewField +="Alevin de Invierno";
    if (item.json.fields["Infantil Invierno"][i] === true) {
      item.json.myNewField +="Infantil de Invierno";
    i = i+1