# Two loops with output

**URL:** <https://discourse.nodered.org/t/two-loops-with-output/1696>\
**Category:** General\
**Created:** [19 July 2018 14:28 UTC](https://discourse.nodered.org/t/two-loops-with-output/1696 "2018-07-19T14:28:40Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![lucky4ever2](https://avatars.discourse-cdn.com/v4/letter/l/eb8c5e/32.png) [@lucky4ever2](https://discourse.nodered.org/u/lucky4ever2)\
**Post date:** [19 July 2018 14:28 UTC](https://discourse.nodered.org/t/two-loops-with-output/1696/1 "2018-07-19T14:28:40Z")

</div>

> var i;  
> var j;  
> var nr = 1;
> 
> for (i = 0; i \< msg.payload.TripList.Trip.length; i++) {  
> output = 'Verbindung ' + nr++;  
> for (j = 0; j \< msg.payload.TripList.Trip[i].LegList[0].Leg.length; j++) {
> 
> ```
> output += "\r\n-----\r\nZugtyp: " + msg.payload.TripList.Trip[i].LegList[0].Leg[j].$.name + 
> "\r\nStartbahnhof: " + msg.payload.TripList.Trip[i].LegList[0].Leg[j].Origin[0].$.name +
> "\r\nStartzeit: " + msg.payload.TripList.Trip[i].LegList[0].Leg[j].Origin[0].$.time + 
> "\r\nZielbahnhof: " + msg.payload.TripList.Trip[i].LegList[0].Leg[j].Destination[0].$.name + 
> "\r\nAnkuftszeit: " + msg.payload.TripList.Trip[i].LegList[0].Leg[j].Destination[0].$.time;
> }
> msg.payload.type = 'message'
> msg.payload.chatId = msg.originalMessage.from.id;
> msg.payload.content = output
> 
> ```
> 
> return msg;  
> [//node.send](https://node.send)(msg);  
> }

With node.send (msg); I get one message per trip.  
Unfortunately asynchronous.  
With "return msg" I get only the first message of the array. The loop then seems to end. How do I get the complete array synchronized?

---

<div class="post-metadata">

**Author:** ![arneym](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/arneym/32/54_2.png) [@arneym](https://discourse.nodered.org/u/arneym)\
**Post date:** [19 July 2018 14:35 UTC](https://discourse.nodered.org/t/two-loops-with-output/1696/2 "2018-07-19T14:35:50Z")

</div>

Here is a link for sending multiple messages:  
[https://nodered.org/docs/writing-functions#multiple-messages](https://nodered.org/docs/writing-functions#multiple-messages)

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [19 July 2018 14:57 UTC](https://discourse.nodered.org/t/two-loops-with-output/1696/3 "2018-07-19T14:57:12Z")

</div>

> [@lucky4ever2](#):
>
> How do I get the complete array synchronized?

I don't understand what you mean by synchronized, using node.send() it should send one message each time it goes round the loop. If that is not what you want then what do you want?

---

<div class="post-metadata">

**Author:** ![lucky4ever2](https://avatars.discourse-cdn.com/v4/letter/l/eb8c5e/32.png) [@lucky4ever2](https://discourse.nodered.org/u/lucky4ever2)\
**Post date:** [19 July 2018 15:44 UTC](https://discourse.nodered.org/t/two-loops-with-output/1696/5 "2018-07-19T15:44:00Z")

</div>

I found the mistake. you're right

---

<div class="post-metadata">

**Author:** ![shrickus](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/shrickus/32/517_2.png) [@shrickus](https://discourse.nodered.org/u/shrickus)\
**Post date:** [19 July 2018 19:00 UTC](https://discourse.nodered.org/t/two-loops-with-output/1696/6 "2018-07-19T19:00:32Z")

</div>

Whenever I see "looping" through data sets, I think about using either a `change` node with a JSONata expression, or a `template` node with mustache-style iteration and substitution...

If you don't mind posting the incoming `msg.payload` as a JSON string, I can give you a couple other solutions, at least for future reference.

---

<div class="post-metadata">

**Author:** ![lucky4ever2](https://avatars.discourse-cdn.com/v4/letter/l/eb8c5e/32.png) [@lucky4ever2](https://discourse.nodered.org/u/lucky4ever2)\
**Post date:** [19 July 2018 20:18 UTC](https://discourse.nodered.org/t/two-loops-with-output/1696/7 "2018-07-19T20:18:42Z")

</div>

hier you find the incoming msg as Json String

[https://wetransfer.com/downloads/45bdb553236c84b75170f7940fccfac520180719201707/aceeeced6d3c0c4f93ab8f8ff445e0f220180719201707/c20539](https://wetransfer.com/downloads/45bdb553236c84b75170f7940fccfac520180719201707/aceeeced6d3c0c4f93ab8f8ff445e0f220180719201707/c20539)
