# for-Loop output questions

**URL:** <https://discourse.nodered.org/t/for-loop-output-questions/94521>\
**Category:** General\
**Created:** [10 January 2025 23:09 UTC](https://discourse.nodered.org/t/for-loop-output-questions/94521 "2025-01-10T23:09:19Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![11awe](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/11awe/32/97928_2.png) [@11awe](https://discourse.nodered.org/u/11awe)\
**Post date:** [10 January 2025 23:09 UTC](https://discourse.nodered.org/t/for-loop-output-questions/94521/1 "2025-01-10T23:09:19Z")

</div>

Hi community,

can you please help me with some issues regarding the loop-output? I want to send customized birthday wishes via contribute "dynamic eMail" once a day. Data are drawn from a spreadsheet as an array.  
I want to run through the array, comparing each **msg.payload[i].Geburtsdatum** with actual date. (The strings to compare follow exactly the same format.)

I think I should put the "return msg" (or node.send?) within the loop to create an input for email-node with every new match. What would the expression be like?  
Do I need to output an array containing the flow-vars or something like a "ping" for the email-node because the flows-vars are dynamically set with every loop?

Does the loop stop after mismatch and do I need an "else" with "continue" to go ahead through the list?  
Thanks for your support.

![grafik](https://us1.discourse-cdn.com/flex026/uploads/nodered/optimized/3X/1/b/1b86d635719c344552f80c3df4a64c017c5fe75d_2_690x32.png)

```auto
const Liste = [];
var Anrede;
var Nachname;
var Mail;

for (let i = 0; i <= Liste.length; i++) 
{  
    if (flow.get('datum_heute') === msg.payload[i].Geburtsdatum)
    
    {  
        flow.set('Mail', msg.payload[i].Mail)
        flow.set('Anrede', msg.payload[i].Anrede)
        flow.set('Nachname', msg.payload[i].Nachname)
    }    

}
return msg;

```

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [10 January 2025 23:55 UTC](https://discourse.nodered.org/t/for-loop-output-questions/94521/2 "2025-01-10T23:55:38Z")

</div>

Hi and welcome to the forum

You seem to be over using the function nodes and context variable. The flow you have shown in the image seems over complicated. You should try to pass data in the message object. Try to avoid context unless it is absolutely needed.

Node-red is a low code plateform, and what you require to do can be done with a split and switch node.  
e.g

```auto
[{"id":"5659df32a49fb39c","type":"inject","z":"667cec54c048503c","name":"","props":[{"p":"payload"},{"p":"date","v":"2025/01/12","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[{\"Geburtsdatum\":\"2025/01/11\",\"Mail\":\"abc@cde.com\",\"Anrede\":\"Alles Gute zum Geburtstag, mögen Sie Reich und Reich regieren\",\"Vorname\":\"Ryker\"},{\"Geburtsdatum\":\"2025/01/12\",\"Mail\":\"abc@cde.com\",\"Anrede\":\"Alles Gute zum Geburtstag, möge Ihr Leben mit Glück und Erfolg gesegnet sein\",\"Vorname\":\"Felix\"}]","payloadType":"json","x":432.1991271972656,"y":1172.6343994140625,"wires":[["530d65e03f140596"]]},{"id":"530d65e03f140596","type":"split","z":"667cec54c048503c","name":"","splt":"\\n","spltType":"str","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"","property":"payload","x":590,"y":1180,"wires":[["8ac201f1ab539450"]]},{"id":"8ac201f1ab539450","type":"switch","z":"667cec54c048503c","name":"","property":"payload.Geburtsdatum","propertyType":"msg","rules":[{"t":"eq","v":"date","vt":"msg"}],"checkall":"true","repair":false,"outputs":1,"x":710,"y":1180,"wires":[["984cf197480bc4af"]]},{"id":"984cf197480bc4af","type":"debug","z":"667cec54c048503c","name":"debug 2579","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":870,"y":1180,"wires":[]}]

```

[How to import/export a flow](https://nodered.org/docs/user-guide/editor/workspace/import-export)

---

<div class="post-metadata">

**Author:** ![Buckskin](https://avatars.discourse-cdn.com/v4/letter/b/b9e5f3/32.png) [@Buckskin](https://discourse.nodered.org/u/Buckskin)\
**Post date:** [11 January 2025 00:20 UTC](https://discourse.nodered.org/t/for-loop-output-questions/94521/3 "2025-01-11T00:20:06Z")

</div>

Not exactly sure why some of this is present but a simple version

```auto
const Liste = (Array.isArray(msg.payload)) ? msg.payload : [] // Validate msg.payload as an Array
const currentDate = flow.get('datum_heute') // Why is this in a flow variable?

for (let i = 0; i < Liste.length; i++) { // Note i < Liste.length
    if (currentDate === Liste[i].Geburtsdatum) {
       msg.payload = {...Liste[i]}

       node.send(msg)
    }
    
}

return

```

Just seen E1cid's none Function example

---

<div class="post-metadata">

**Author:** ![11awe](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/11awe/32/97928_2.png) [@11awe](https://discourse.nodered.org/u/11awe)\
**Post date:** [12 January 2025 00:18 UTC](https://discourse.nodered.org/t/for-loop-output-questions/94521/4 "2025-01-12T00:18:57Z")

</div>

Thanks guys, worked pretty well. Appreciate!

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [26 January 2025 00:19 UTC](https://discourse.nodered.org/t/for-loop-output-questions/94521/5 "2025-01-26T00:19:13Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
