# How to pass the array to the loop

**URL:** https://discourse.nodered.org/t/how-to-pass-the-array-to-the-loop/32381
**Category:** General
**Created:** [3 September 2020 05:11 UTC](https://discourse.nodered.org/t/how-to-pass-the-array-to-the-loop/32381 "2020-09-03T05:11:49Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![minhhn0205](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/minhhn0205/32/29062_2.png) [@minhhn0205](https://discourse.nodered.org/u/minhhn0205)
#### Post date: [3 September 2020 05:11 UTC](https://discourse.nodered.org/t/how-to-pass-the-array-to-the-loop/32381/1 "2020-09-03T05:11:50Z")

</div>

Hello Everyone,

I'm completely new to node-red.  
I am trying to create a flow in which i have to loop while condition and  
within loop i need to add sub flows based on value of loop.  
_※sub flows are the same handling._

Input

- A zip file that inside contains many text files

Output

- The result file contains the processed content of each txt file in the input ZIP file, such as comparison, calculation and editing.

I'm thinking of the draf like this:

 ![node-red-loop-using-node](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/a/6a5eb9e958a6efa15d22eb84a7b5b4a2d8c5addc.png)

```auto
[{"id":"a5510213.ab69c","type":"subflow","name":"Subflow 1","info":"","in":[],"out":[]},{"id":"43e653d8.4eb50c","type":"function","z":"a5510213.ab69c","name":"","func":"\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":190,"y":80,"wires":[[]]},{"id":"18febc57.82b2a4","type":"switch","z":"a5510213.ab69c","name":"","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":220,"y":120,"wires":[[]]},{"id":"603ee840.f02c08","type":"template","z":"3b501765.6a7958","name":"i=0","field":"i","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"0","x":510,"y":520,"wires":[["17301876.45fad8"]]},{"id":"17301876.45fad8","type":"switch","z":"3b501765.6a7958","name":"i<4","property":"i","propertyType":"msg","rules":[{"t":"lt","v":"4","vt":"str"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":790,"y":520,"wires":[["781cc82b.bc4508"],["bc8de519.bf7fb8"]]},{"id":"de118552.608c38","type":"function","z":"3b501765.6a7958","name":"i++","func":"msg.i++;\n\nreturn msg;","outputs":1,"noerr":0,"x":950,"y":380,"wires":[["17301876.45fad8"]]},{"id":"912db2b0.8768b","type":"debug","z":"3b501765.6a7958","name":"msg end","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1260,"y":560,"wires":[]},{"id":"ae6ab5c2.fee078","type":"debug","z":"3b501765.6a7958","name":"msg output","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1270,"y":480,"wires":[]},{"id":"1f381e41.ded552","type":"inject","z":"3b501765.6a7958","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":"","topic":"","payload":"","payloadType":"str","x":390,"y":520,"wires":[["603ee840.f02c08"]]},{"id":"21b81be3.dd31f4","type":"comment","z":"3b501765.6a7958","name":"↓ output 4 times","info":"","x":1280,"y":440,"wires":[]},{"id":"ab22648b.1eff08","type":"comment","z":"3b501765.6a7958","name":"conditional for while ↑","info":"","x":760,"y":560,"wires":[]},{"id":"e138d0d7.649ce","type":"comment","z":"3b501765.6a7958","name":"↓ init value i to increment","info":"","x":570,"y":480,"wires":[]},{"id":"d10b2b36.526988","type":"comment","z":"3b501765.6a7958","name":"↓ count up i","info":"","x":970,"y":340,"wires":[]},{"id":"bc8de519.bf7fb8","type":"template","z":"3b501765.6a7958","name":"Post-loop processing","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"End of loop","x":1020,"y":560,"wires":[["912db2b0.8768b"]]},{"id":"781cc82b.bc4508","type":"template","z":"3b501765.6a7958","name":"Generate result output","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"Value i = {{i}}","x":1020,"y":480,"wires":[["ae6ab5c2.fee078","de118552.608c38"]]},{"id":"d4664aee.ff01c8","type":"inject","z":"3b501765.6a7958","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"str","x":310,"y":740,"wires":[["6fbc670f.1f89b8"]]},{"id":"9d77fa16.931b98","type":"zip","z":"3b501765.6a7958","name":"","mode":"decompress","filename":"","outasstring":false,"x":540,"y":740,"wires":[["b12057db.c97bd8"]]},{"id":"dc2e094d.3e4c88","type":"subflow:a5510213.ab69c","z":"3b501765.6a7958","name":"","env":[],"x":800,"y":740,"wires":[]},{"id":"fde08b64.a893b8","type":"comment","z":"3b501765.6a7958","name":"↑ file content: comparison, calculation, editing,...","info":"","x":920,"y":780,"wires":[]},{"id":"b12057db.c97bd8","type":"debug","z":"3b501765.6a7958","name":"msg zip","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":690,"y":660,"wires":[]},{"id":"77cad8a2.aa1348","type":"comment","z":"3b501765.6a7958","name":"↑ output array file txt","info":"","x":578,"y":780,"wires":[]},{"id":"6fbc670f.1f89b8","type":"file in","z":"3b501765.6a7958","name":"","filename":"/Temp/FOLDER_TEXT.zip","format":"","chunk":false,"sendError":false,"encoding":"none","x":410,"y":640,"wires":[["9d77fa16.931b98"]]}]

```

■What has been done:

- Create `while` loop using node of Node-RED with the value of the loop condition fixed is `4`
- The extracted input zip file the result is an array `msg.payload : array[3]` (★)

■Problem:

- I don't know how to put array in (★) instead of fixed value `4` of `while` loop and make it work

Any idea will be great.  
Thanks.

---

<div class="post-metadata">

### Author: ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)
#### Post date: [3 September 2020 05:23 UTC](https://discourse.nodered.org/t/how-to-pass-the-array-to-the-loop/32381/2 "2020-09-03T05:23:00Z")

</div>

OK, quick question:

~~Is the number of loops known?~~

Silly question.

The top `inject` node. That starts things happening. Though I guess the bottom one does stuff too.

Is the number of loops you need to do known when you press the top `inject` button?  
Actually, why two `inject` buttons?

I need to know where in the scheme of things those two `inject` nodes are pressed.

I can show you a way to control the `switch` node as is if you want.

Keeping to your flow set up / layout here is a _trick_ you can do.

```auto
[{"id":"a5eb3e22.30b0a","type":"inject","z":"68e3655f.b606b4","name":"","topic":"","payload":"1","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":200,"y":1820,"wires":[["3ee76551.6d380a"]]},{"id":"f3c4e495.867ca","type":"inject","z":"68e3655f.b606b4","name":"","topic":"","payload":"2","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":200,"y":1860,"wires":[["3ee76551.6d380a"]]},{"id":"780d5119.58eef8","type":"inject","z":"68e3655f.b606b4","name":"","topic":"","payload":"3","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":200,"y":1900,"wires":[["3ee76551.6d380a"]]},{"id":"aed35ff7.1c31b8","type":"inject","z":"68e3655f.b606b4","name":"","topic":"","payload":"4","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":200,"y":1940,"wires":[["3ee76551.6d380a"]]},{"id":"cd3e8854.7fea9","type":"inject","z":"68e3655f.b606b4","name":"","topic":"","payload":"5","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":200,"y":1980,"wires":[["3ee76551.6d380a"]]},{"id":"29b8e918.8a8e76","type":"inject","z":"68e3655f.b606b4","name":"","topic":"","payload":"6","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":200,"y":2020,"wires":[["3ee76551.6d380a"]]},{"id":"2546c770.a58fc","type":"inject","z":"68e3655f.b606b4","name":"","topic":"","payload":"7","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":200,"y":2060,"wires":[["3ee76551.6d380a"]]},{"id":"19ac02a9.39538d","type":"comment","z":"68e3655f.b606b4","name":"Determined else where in the flow.","info":"","x":230,"y":1780,"wires":[]},{"id":"3ee76551.6d380a","type":"change","z":"68e3655f.b606b4","name":"flow context loop_count","rules":[{"t":"set","p":"loop_count","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":430,"y":1940,"wires":[[]]},{"id":"77848dd5.e1e62c","type":"switch","z":"68e3655f.b606b4","name":"i<4","property":"i","propertyType":"msg","rules":[{"t":"lt","v":"loop_count","vt":"flow"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":510,"y":1690,"wires":[["bf585193.9715a8"],["9a914e62.49a298"]]}]

```

But you **MUST** inject a value before you run the flow.

The `7` options (as examples) is set else where in the flow, where you know how many times you need to loop.

That stores the value in a _flow context_ which is a neat thing I took a long time to get my head around.

Then the `switch` node looks at the value and compares it to the _flow context_ value.  
Here I've called it `loop_count` for clarity.

There are three kinds of context - of which I know:

Node context. So it is only seen in that node. Handy in `function` nodes.  
Flow context. Which is visible to that ... _flow_. (or tab) in the editor screen.  
and lastly  
Global context. Which is .... global.

They are like an overlying variable you can use in scenarios like this.

Hope that helps.

---

<div class="post-metadata">

### Author: ![minhhn0205](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/minhhn0205/32/29062_2.png) [@minhhn0205](https://discourse.nodered.org/u/minhhn0205)
#### Post date: [3 September 2020 06:09 UTC](https://discourse.nodered.org/t/how-to-pass-the-array-to-the-loop/32381/3 "2020-09-03T06:09:10Z")

</div>

Sorry for the confusing explanation.  
The picture above is just a draf illustrating what I was thinking.  
The end result is aggregated into an `inject` node.

I want to pass an array of text files into a loop like `msg.payload.length` instead of the fixed `4` value of the `while` loop.

Array of length 3, but in the while loop is not received...

 ![pass-array-to-loop](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/d/0d7c08f126d4253d0a71cf22c40ddc81baaad406.png)

```auto
[{"id":"6683e387.1412ec","type":"template","z":"3b501765.6a7958","name":"i=0","field":"i","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"0","x":970,"y":1840,"wires":[["5d38e308.fc926c"]]},{"id":"b06fdcae.0566f","type":"switch","z":"3b501765.6a7958","name":"i<msg.payload","property":"i","propertyType":"msg","rules":[{"t":"lt","v":"payload","vt":"msg"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":1380,"y":1840,"wires":[["ad7b00b5.afe92"],["1d1d7b28.2d9f95"]]},{"id":"1b90939b.ddff3c","type":"function","z":"3b501765.6a7958","name":"i++","func":"msg.i++;\n\nreturn msg;","outputs":1,"noerr":0,"x":1550,"y":1680,"wires":[["b06fdcae.0566f"]]},{"id":"72ff57b2.8d9b98","type":"debug","z":"3b501765.6a7958","name":"msg end","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1900,"y":1880,"wires":[]},{"id":"6fd1f00e.0c2f2","type":"debug","z":"3b501765.6a7958","name":"msg output","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1910,"y":1800,"wires":[]},{"id":"6eb1638.8492e9c","type":"inject","z":"3b501765.6a7958","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":"","topic":"","payload":"","payloadType":"str","x":730,"y":1680,"wires":[["1e822a01.417016"]]},{"id":"f56ab556.176988","type":"comment","z":"3b501765.6a7958","name":"conditional for while ↑","info":"","x":1400,"y":1880,"wires":[]},{"id":"a1f1f218.97fdd","type":"comment","z":"3b501765.6a7958","name":"↓ init value i to increment","info":"","x":1030,"y":1800,"wires":[]},{"id":"eac8b268.578e","type":"comment","z":"3b501765.6a7958","name":"↓ count up i","info":"","x":1570,"y":1640,"wires":[]},{"id":"1d1d7b28.2d9f95","type":"template","z":"3b501765.6a7958","name":"Post-loop processing","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"End of loop","x":1660,"y":1880,"wires":[["72ff57b2.8d9b98"]]},{"id":"ad7b00b5.afe92","type":"template","z":"3b501765.6a7958","name":"Generate result output","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"Value i = {{i}}","x":1660,"y":1800,"wires":[["1b90939b.ddff3c","6fd1f00e.0c2f2"]]},{"id":"79958d94.6dc6a4","type":"zip","z":"3b501765.6a7958","name":"","mode":"decompress","filename":"","outasstring":false,"x":790,"y":1840,"wires":[["73a018f3.085488","6683e387.1412ec"]]},{"id":"73a018f3.085488","type":"debug","z":"3b501765.6a7958","name":"msg zip","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":980,"y":1940,"wires":[]},{"id":"6d0dc499.b9d6ec","type":"comment","z":"3b501765.6a7958","name":"output array file txt ↑","info":"","x":750,"y":1880,"wires":[]},{"id":"1e822a01.417016","type":"file in","z":"3b501765.6a7958","name":"","filename":"/Temp/FOLDER_TEXT.zip","format":"","chunk":false,"sendError":false,"encoding":"none","x":970,"y":1680,"wires":[["79958d94.6dc6a4"]]},{"id":"5d38e308.fc926c","type":"function","z":"3b501765.6a7958","name":"","func":"msg.payload = msg.payload.length;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":1140,"y":1880,"wires":[["b06fdcae.0566f","92139f11.34ea8"]]},{"id":"92139f11.34ea8","type":"debug","z":"3b501765.6a7958","name":"msg function","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1310,"y":1940,"wires":[]}]

```

---

<div class="post-metadata">

### Author: ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)
#### Post date: [3 September 2020 06:34 UTC](https://discourse.nodered.org/t/how-to-pass-the-array-to-the-loop/32381/4 "2020-09-03T06:34:49Z")

</div>

Just a note, node-red is used to get a message from point a to point b with manipulation along the way: loops should be avoided when possible.

Why is there a "need" for a loop ?

---

<div class="post-metadata">

### Author: ![minhhn0205](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/minhhn0205/32/29062_2.png) [@minhhn0205](https://discourse.nodered.org/u/minhhn0205)
#### Post date: [3 September 2020 07:23 UTC](https://discourse.nodered.org/t/how-to-pass-the-array-to-the-loop/32381/5 "2020-09-03T07:23:32Z")

</div>

> [@bakman2](#):
>
> Why is there a "need" for a loop ?

Need a loop because corresponding to the processing of each TXT file, if it fails, it will do other things such as _output the file to the Error folder_ (1), _send mail to report the error file_ (2), ... and this also needs to be done with the nodes of Node-RED.

If there is no separate loop, but write the loop in `function` node, things like (1), (2), ... cannot be done by node of Node-RED.

---

<div class="post-metadata">

### Author: ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)
#### Post date: [3 September 2020 07:45 UTC](https://discourse.nodered.org/t/how-to-pass-the-array-to-the-loop/32381/6 "2020-09-03T07:45:20Z")

</div>

You dont really need a loop - you need to change your perspective 😉

Perhaps this would work for you (I am uncertain what you intend to do with the templates so it may not work for you)

#### Look maa - no loops ...

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/7/a792b9081f1907b3c7616114aeec653aae0235ed.png)

#### results...

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/4/446ab5bcab23d05df6aa56ef5bba0b8d09d53e53.png)

```auto
[{"id":"910ee8fb.f36198","type":"debug","z":"ea150be4.370218","name":"template2","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":1220,"y":260,"wires":[]},{"id":"1ff87870.2d6228","type":"inject","z":"ea150be4.370218","name":"","topic":"","payload":"","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":"","x":430,"y":120,"wires":[["57313806.5355e8"]]},{"id":"5420ca87.6bc1d4","type":"template","z":"ea150be4.370218","name":"Post-loop processing","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"Processed {{payload.length}} files","x":1160,"y":200,"wires":[["910ee8fb.f36198"]]},{"id":"9706d9df.0f3b38","type":"template","z":"ea150be4.370218","name":"Generate result output","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"This filename is \"{{payload.filename}}\"","x":780,"y":200,"wires":[["62e49bf8.fff2c4","21bf6987.4c4ff6"]]},{"id":"db970231.874ab","type":"zip","z":"ea150be4.370218","name":"","mode":"decompress","filename":"","outasstring":false,"x":430,"y":200,"wires":[["e9d2d77f.490a48"]]},{"id":"57313806.5355e8","type":"file in","z":"ea150be4.370218","name":"","filename":"c:/temp/images.zip","format":"","chunk":false,"sendError":false,"encoding":"none","x":650,"y":120,"wires":[["db970231.874ab"]]},{"id":"e9d2d77f.490a48","type":"split","z":"ea150be4.370218","name":"","splt":"\\n","spltType":"str","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"topic","x":580,"y":200,"wires":[["9706d9df.0f3b38","6e7dd561.f426cc"]]},{"id":"62e49bf8.fff2c4","type":"join","z":"ea150be4.370218","name":"","mode":"auto","build":"string","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":"false","timeout":"","count":"","reduceRight":false,"x":970,"y":200,"wires":[["5420ca87.6bc1d4","67706051.fe865"]]},{"id":"6e7dd561.f426cc","type":"debug","z":"ea150be4.370218","name":"split","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":610,"y":260,"wires":[]},{"id":"21bf6987.4c4ff6","type":"debug","z":"ea150be4.370218","name":"template1","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":820,"y":260,"wires":[]},{"id":"67706051.fe865","type":"debug","z":"ea150be4.370218","name":"join","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":1010,"y":260,"wires":[]}]

```

---

<div class="post-metadata">

### Author: ![minhhn0205](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/minhhn0205/32/29062_2.png) [@minhhn0205](https://discourse.nodered.org/u/minhhn0205)
#### Post date: [3 September 2020 07:59 UTC](https://discourse.nodered.org/t/how-to-pass-the-array-to-the-loop/32381/7 "2020-09-03T07:59:57Z")

</div>

Thank you for your support!

Need a loop because corresponding to the processing of each TXT file, if it fails, it will do other things such as _output the file to the Error folder_ (1), _send mail to report the error file_ (2), ... and this also needs to be done with the nodes of Node-RED.

If there is no separate loop, but write the loop in `function` node, things like (1), (2), ... cannot be done by node of Node-RED.

---

<div class="post-metadata">

### Author: ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)
#### Post date: [3 September 2020 08:07 UTC](https://discourse.nodered.org/t/how-to-pass-the-array-to-the-loop/32381/8 "2020-09-03T08:07:09Z")

</div>

> [@minhhn0205](#):
>
> Need a loop because corresponding to the processing of each TXT file

Again, it is VERY likely **you do NOT need to loop**. You need to learn node-red.

The "`generate result output`" node could be **ANYTHING**. It could be bunch of nodes that process a file, check it for errors, raising an error, processing data - it could be anything. It could be a subflow that does a whole bunch of pre-processing.

Perhaps you do not realise that split node generates new `msg` objects for you to work on individually (just like a loop would). After the split node, you might as well pretend you are a "loop" as you get access to the individual parts of the array to do as you please.

So again - why **MUST** there be a loop???

  
  

> [@minhhn0205](#):
>
> If there is no separate loop, but write the loop in `function` node, things like (1), (2), ... cannot be done by node of Node-RED.

This doesnt make a whole lot of sense. Can you explain with details?

---

<div class="post-metadata">

### Author: ![minhhn0205](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/minhhn0205/32/29062_2.png) [@minhhn0205](https://discourse.nodered.org/u/minhhn0205)
#### Post date: [3 September 2020 08:31 UTC](https://discourse.nodered.org/t/how-to-pass-the-array-to-the-loop/32381/9 "2020-09-03T08:31:30Z")

</div>

> [@Steve-Mcl](#):
>
> This doesnt make a whole lot of sense. Can you explain with details?

I am trying to write a flow that will extract a zip file containing TXT files, from the extracted TXT files, check the contents of each TXT file.

If the checking file content fails, it will move the TXT file to the Error folder (1), send a mail report to the TXT file (2), ... and continue to process the next TXT file.  
Things like (1), (2) I also want to do with Node-RED's node

So I think there's a loop for all of this. As a newbie to Node-RED, I do not know what best practices should be in this case. Looking forward to your guidance!

---

<div class="post-metadata">

### Author: ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)
#### Post date: [3 September 2020 08:37 UTC](https://discourse.nodered.org/t/how-to-pass-the-array-to-the-loop/32381/10 "2020-09-03T08:37:27Z")

</div>

@minhhn0205

I know this can be very frustrating and I know people think differently.

What you have said there is still _vague_.  
Sorry.

So, reading what you said:

You have a zip file. (contents TXT files.)

@Steve-Mcl  
I only sort of get what you mean, but alas that too is a bit _vague_ for me, let alone a newbie.

I am a bit stuck how to write a flow to extract multiple files from one source file; check each file and act accordingly.

---

<div class="post-metadata">

### Author: ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)
#### Post date: [3 September 2020 08:58 UTC](https://discourse.nodered.org/t/how-to-pass-the-array-to-the-loop/32381/11 "2020-09-03T08:58:51Z")

</div>

What are you struggling with?

He says he wants to loop files - I said its not required (so did @bakman2)

> [@minhhn0205](#):
>
> I am trying to write a flow that will extract a zip file containing TXT files, from the extracted TXT files, check the contents of each TXT file

Like this (no loop)

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/7/e7d1384734f9e182ea2cf1b90ebe0c2b08d93aa7.png)

I dont think you are understanding the nature of node-red and messages.

After the SPLIT node, you have your individual files - no loop needed

---

<div class="post-metadata">

### Author: ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)
#### Post date: [3 September 2020 09:00 UTC](https://discourse.nodered.org/t/how-to-pass-the-array-to-the-loop/32381/12 "2020-09-03T09:00:35Z")

</div>

Oops, I just realised I didn't really complete what I was saying.

Do you have multiple ZIP files which need to be unZipped?

If you do, that is another level of _looping_ you may not have noticed.

So: Let's say you have only one.

You want to unzip it, examine the files in it and do things depending on what happens.  
And you are doing that by looping.

Rather than that how about this method:  
1 - Test the zip integrity. Good/Bad.  
2 - Extract files to a temporary place.  
3 - Build an index of the files. (You could call this a loop.)  
4 - Test each of the files in the index. Act accordingly.

So, I guess you could say is that the _index_ is a loop.  
But that is at a more basic level for Node-Red, so you can do functions (zip stuff) on the files and see the results. (Pass / Fail).

---

<div class="post-metadata">

### Author: ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)
#### Post date: [3 September 2020 09:00 UTC](https://discourse.nodered.org/t/how-to-pass-the-array-to-the-loop/32381/13 "2020-09-03T09:00:44Z")

</div>

> [@Trying\_to\_learn](#):
>
> I am a bit stuck how to write a flow to extract multiple files from one source file; check each file and act accordingly.

that has nothing to do with the OPs issue but for you information - look at the debug output I posted

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/f/8f1644157a2763c0f36d1ade031ca8bc51f3102f.png)

the files are split out of the ZIP payload.

---

<div class="post-metadata">

### Author: ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)
#### Post date: [3 September 2020 09:01 UTC](https://discourse.nodered.org/t/how-to-pass-the-array-to-the-loop/32381/14 "2020-09-03T09:01:22Z")

</div>

> [@Steve-Mcl](#):
>
> The " `generate result output` " node could be **ANYTHING**.

I'll be brief:

Keh?

---

<div class="post-metadata">

### Author: ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)
#### Post date: [3 September 2020 09:04 UTC](https://discourse.nodered.org/t/how-to-pass-the-array-to-the-loop/32381/15 "2020-09-03T09:04:56Z")

</div>

> [@Trying\_to\_learn](#):
>
> I'll be brief:
> 
> Keh?

um - he wants to process the file data.

> [@Steve-Mcl](#):
>
> The " `generate result output` " node could be **ANYTHING**

If you looked at or imported the flow I posted - you will see...

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/0/f058c2effc840b3c34e097713fc0cd1684599753.png)

---

<div class="post-metadata">

### Author: ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)
#### Post date: [3 September 2020 09:06 UTC](https://discourse.nodered.org/t/how-to-pass-the-array-to-the-loop/32381/16 "2020-09-03T09:06:02Z")

</div>

(I'm kind of glad I am only learning. Or trying to.)

I'm outa here.

---

<div class="post-metadata">

### Author: ![minhhn0205](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/minhhn0205/32/29062_2.png) [@minhhn0205](https://discourse.nodered.org/u/minhhn0205)
#### Post date: [3 September 2020 09:41 UTC](https://discourse.nodered.org/t/how-to-pass-the-array-to-the-loop/32381/17 "2020-09-03T09:41:17Z")

</div>

Since I am new to Node-RED, I am looking to practice Node-RED node manipulation.

I would like to output like your sample:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/7/a792b9081f1907b3c7616114aeec653aae0235ed.png)  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/4/446ab5bcab23d05df6aa56ef5bba0b8d09d53e53.png)

by doing a loop like below, but do not know where is wrong and the results are not correct...  
Please tell me! Thanks.

 ![description_img](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/7/07bc06c89495b841feba6324e053a4565b3f535a.png)

```auto
[{"id":"6683e387.1412ec","type":"template","z":"3b501765.6a7958","name":"i=0","field":"i","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"0","x":1070,"y":1280,"wires":[["b06fdcae.0566f"]]},{"id":"b06fdcae.0566f","type":"switch","z":"3b501765.6a7958","name":"i<msg.payload","property":"i","propertyType":"msg","rules":[{"t":"lt","v":"payload","vt":"msg"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":1360,"y":1280,"wires":[["ad7b00b5.afe92","2e2306e9.7c408a"],["1d1d7b28.2d9f95"]]},{"id":"1b90939b.ddff3c","type":"function","z":"3b501765.6a7958","name":"i++","func":"msg.i++;\n\nreturn msg;","outputs":1,"noerr":0,"x":1530,"y":1120,"wires":[["b06fdcae.0566f"]]},{"id":"72ff57b2.8d9b98","type":"debug","z":"3b501765.6a7958","name":"msg end","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1840,"y":1320,"wires":[]},{"id":"6fd1f00e.0c2f2","type":"debug","z":"3b501765.6a7958","name":"msg output","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1870,"y":1240,"wires":[]},{"id":"6eb1638.8492e9c","type":"inject","z":"3b501765.6a7958","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":"","topic":"","payload":"","payloadType":"str","x":710,"y":1120,"wires":[["1e822a01.417016"]]},{"id":"f56ab556.176988","type":"comment","z":"3b501765.6a7958","name":"conditional for while ↑","info":"","x":1360,"y":1320,"wires":[]},{"id":"a1f1f218.97fdd","type":"comment","z":"3b501765.6a7958","name":"↓ init value i to increment","info":"","x":1130,"y":1240,"wires":[]},{"id":"eac8b268.578e","type":"comment","z":"3b501765.6a7958","name":"↓ count up i","info":"","x":1550,"y":1080,"wires":[]},{"id":"1d1d7b28.2d9f95","type":"template","z":"3b501765.6a7958","name":"Post-loop processing","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"End of loop","x":1620,"y":1320,"wires":[["72ff57b2.8d9b98"]]},{"id":"ad7b00b5.afe92","type":"template","z":"3b501765.6a7958","name":"Generate result output","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"This filename is {{payload.filename}}","x":1620,"y":1260,"wires":[["1b90939b.ddff3c","6fd1f00e.0c2f2"]]},{"id":"79958d94.6dc6a4","type":"zip","z":"3b501765.6a7958","name":"","mode":"decompress","filename":"","outasstring":false,"x":770,"y":1280,"wires":[["73a018f3.085488","5d38e308.fc926c"]]},{"id":"73a018f3.085488","type":"debug","z":"3b501765.6a7958","name":"msg zip","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":900,"y":1360,"wires":[]},{"id":"6d0dc499.b9d6ec","type":"comment","z":"3b501765.6a7958","name":"output array file txt ↑","info":"","x":730,"y":1320,"wires":[]},{"id":"1e822a01.417016","type":"file in","z":"3b501765.6a7958","name":"","filename":"/Temp/FOLDER_TEXT.zip","format":"","chunk":false,"sendError":false,"encoding":"none","x":950,"y":1120,"wires":[["79958d94.6dc6a4"]]},{"id":"5d38e308.fc926c","type":"function","z":"3b501765.6a7958","name":"","func":"msg.payload = msg.payload.length;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":920,"y":1280,"wires":[["92139f11.34ea8","6683e387.1412ec"]]},{"id":"92139f11.34ea8","type":"debug","z":"3b501765.6a7958","name":"msg function","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1090,"y":1340,"wires":[]},{"id":"2e2306e9.7c408a","type":"debug","z":"3b501765.6a7958","name":"msg switch","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1550,"y":1220,"wires":[]}]

```

---

<div class="post-metadata">

### Author: ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)
#### Post date: [3 September 2020 09:49 UTC](https://discourse.nodered.org/t/how-to-pass-the-array-to-the-loop/32381/18 "2020-09-03T09:49:04Z")

</div>

OK, so you can see the output in my debug works - yet you still want to use a loop - I dont understand.

  

you also say

> [@minhhn0205](#):
>
> I do not know what best practices should be in this case. Looking forward to your guidance

and

> [@minhhn0205](#):
>
> I am looking to practice Node-RED

My advice on best practice in this particular situation is...

## "DO, NOT, USE, A, LOOP!"
  

* * *

> [@minhhn0205](#):
>
> Since I am new to Node-RED

I recommend watching this playlist: [Node-RED Essentials](https://www.youtube.com/playlist?list=PLyNBB9VCLmo1hyO-4fIZ08gqFcXBkHy-6). It is by the developers of node-red. They're nice & short and to the point. You will understand a whole lot more in less than an hour.

I strongly recommend you watch these videos.

* * *

If you STILL want to do a loop - I am certain someone else can assist.

---

<div class="post-metadata">

### Author: ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)
#### Post date: [3 September 2020 10:23 UTC](https://discourse.nodered.org/t/how-to-pass-the-array-to-the-loop/32381/19 "2020-09-03T10:23:19Z")

</div>

Kiyo, maybe I can explain what @Steve-Mcl has been trying to get across.

The zip node will unzip the file you provide and output a msg. In that msg will be msg.payload that will contain an array where each item in the zip file will have an entry. For example, I created a zip file of two text files. If you look at the output, you will see that msg.payload is an array of two elements:  
 ![Screen Shot 2020-09-03 at 6.08.11 AM](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/d/8d9cf150a24d53c6604a96d68f2eabae2b998cbe.png)

Each array item contains the filename and the content of that file  
msg.payload[0].filename  
msg.payload[0].payload

Now if you send that msg into a split node, the split node wil output two messages, one for each file that was in the zip file.

At this point you can add logic to check each file and set a status in the msg. Then use a `switch` node to examine the status. If it fails, send the message along a 'failed' leg of the flow and it it passes, send it along a 'success' leg of the flow.

Here is an example where the two `link` nodes would go to your code for passing and failing.

 ![Screen Shot 2020-09-03 at 6.21.53 AM](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/b/1b1232735930df78813ffcdc853864c3233f7f7a.png)

---

<div class="post-metadata">

### Author: ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)
#### Post date: [3 September 2020 11:33 UTC](https://discourse.nodered.org/t/how-to-pass-the-array-to-the-loop/32381/20 "2020-09-03T11:33:52Z")

</div>

A post was split to a new topic: [Best way to code this](https://discourse.nodered.org/t/best-way-to-code-this/32397)

[Next page](https://discourse.nodered.org/t/how-to-pass-the-array-to-the-loop/32381.md?page=2)
