# How to repeat a flow based on an array of objetcts

**URL:** <https://discourse.nodered.org/t/how-to-repeat-a-flow-based-on-an-array-of-objetcts/45749>\
**Category:** General\
**Created:** [17 May 2021 04:19 UTC](https://discourse.nodered.org/t/how-to-repeat-a-flow-based-on-an-array-of-objetcts/45749 "2021-05-17T04:19:35Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![ndz](https://avatars.discourse-cdn.com/v4/letter/n/a183cd/32.png) [@ndz](https://discourse.nodered.org/u/ndz)\
**Post date:** [17 May 2021 04:19 UTC](https://discourse.nodered.org/t/how-to-repeat-a-flow-based-on-an-array-of-objetcts/45749/1 "2021-05-17T04:19:36Z")

</div>

Hi, I'm new to node red and i got this project where a need to repeat a flow that does 3 http requests and save information from then.

in the moment my flow is this one

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

in the moment i have this on my template node:

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/5/35710c14080525793d15c7b8f62463011db38dea.png)

I'm using it to change the parameters on my "set data" node and it works fine for just one iten in the array of the template. But i need that this repeats from the 3 itens in the array: code 1, code 2 and code 3.

I've tried using the split node but idk how to save the code in a way that can repeat the request and save the infos i need from there.

Is there any node that could help me solve that? i'm kinda lost in it and if anyone have a link that could help me i woud appreciate it.

Idk if its clear what i intend to do, my english its not the best, im sorry for that.

edit: thats my "set data" node at the moment:

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

---

<div class="post-metadata">

**Author:** ![craigcurtin](https://avatars.discourse-cdn.com/v4/letter/c/94ad74/32.png) [@craigcurtin](https://discourse.nodered.org/u/craigcurtin)\
**Post date:** [17 May 2021 05:04 UTC](https://discourse.nodered.org/t/how-to-repeat-a-flow-based-on-an-array-of-objetcts/45749/2 "2021-05-17T05:04:36Z")

</div>

I guess tell us what the end result is that you are trying to achieve ??

Just guessing but i assume you are wanting to make 3 seperate requests and have the results all come back as one ?

If so do each requst individually, take the result and use a change node on each one to move the msg.payload to (say) msg.payload.req1 etc - you can then use the join node or a function node to put them all back into a single message for further procssing

Craig

---

<div class="post-metadata">

**Author:** ![ndz](https://avatars.discourse-cdn.com/v4/letter/n/a183cd/32.png) [@ndz](https://discourse.nodered.org/u/ndz)\
**Post date:** [17 May 2021 05:13 UTC](https://discourse.nodered.org/t/how-to-repeat-a-flow-based-on-an-array-of-objetcts/45749/3 "2021-05-17T05:13:52Z")

</div>

I need to make 3 requests for each iten in the array and save information from this requests into a flow.set that i already have in my flow.

is that clear?

---

<div class="post-metadata">

**Author:** ![craigcurtin](https://avatars.discourse-cdn.com/v4/letter/c/94ad74/32.png) [@craigcurtin](https://discourse.nodered.org/u/craigcurtin)\
**Post date:** [17 May 2021 05:43 UTC](https://discourse.nodered.org/t/how-to-repeat-a-flow-based-on-an-array-of-objetcts/45749/4 "2021-05-17T05:43:00Z")

</div>

So each request is completely independent of the other requests - it is just the results that you wish to combine together ?

Start off small

Make a single request using the HTTP node and put a debug node on the return , then w can start building it up from there.

Also please read at the top of the forum how to post your flows correctly so others can download and install them to assist you

Craig

---

<div class="post-metadata">

**Author:** ![ndz](https://avatars.discourse-cdn.com/v4/letter/n/a183cd/32.png) [@ndz](https://discourse.nodered.org/u/ndz)\
**Post date:** [17 May 2021 13:05 UTC](https://discourse.nodered.org/t/how-to-repeat-a-flow-based-on-an-array-of-objetcts/45749/6 "2021-05-17T13:05:39Z")

</div>

Yes, each request is completely independent, i'm using just one request here to demonstrate what i need

I'm sending the flow

[flows(6).json](https://discourse.nodered.org/uploads/short-url/hn7A5IVwUTNMSFGNtYFeakM87PO.json) (2.0 KB)

This is working for just one iten in the array of the template, what i need its a way to repeat it to the entire array

---

<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:** [17 May 2021 13:21 UTC](https://discourse.nodered.org/t/how-to-repeat-a-flow-based-on-an-array-of-objetcts/45749/7 "2021-05-17T13:21:20Z")

</div>

Just split the array then move msg.payload.code to msg.linhas.

e.g.

```auto
[{"id":"682b754e.bcf20c","type":"inject","z":"c74669a0.6a34f8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":170,"y":580,"wires":[["7e8223e5.e883dc"]]},{"id":"7e8223e5.e883dc","type":"template","z":"c74669a0.6a34f8","name":"","field":"payload","fieldType":"msg","format":"json","syntax":"plain","template":"[\n {\n \"code\" : \"1\",\n \"CodigoUnidade\" : \"803\"\n },\n {\n \"code\" : \"2\",\n \"CodigoUnidade\" : \"803\"\n },\n {\n \"code\" : \"3\",\n \"CodigoUnidade\" : \"803\"\n }\n]","output":"json","x":330,"y":580,"wires":[["dc91b488.aefc48"]]},{"id":"dc91b488.aefc48","type":"split","z":"c74669a0.6a34f8","name":"","splt":"\\n","spltType":"str","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"","x":470,"y":580,"wires":[["a19706c1.78913"]]},{"id":"a19706c1.78913","type":"change","z":"c74669a0.6a34f8","name":"set data","rules":[{"t":"set","p":"linha","pt":"msg","to":"payload.code","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":440,"y":660,"wires":[["6f18fa2a.d351a4"]]},{"id":"6f18fa2a.d351a4","type":"http request","z":"c74669a0.6a34f8","name":"req - PL","method":"GET","ret":"obj","paytoqs":"ignore","url":"https://60a1a403d2855b00173b2521.mockapi.io/pl-{{linha}}","tls":"","persist":false,"proxy":"","authType":"","x":600,"y":660,"wires":[["1e069998.b34d16"]]},{"id":"1e069998.b34d16","type":"function","z":"c74669a0.6a34f8","name":"","func":"//flow.set('coletas_dia', msg.payload.payload.content.filter(e => e.dataColeta.startsWith('2021-04-04')))\n\nflow.set('coletas_dia', [])\n\n_coletasDia = flow.get('coletas_dia')\n\nmsg.payload.payload.content.filter(e => e.dataColeta.startsWith('2021-04-04')).forEach(i => _coletasDia.push({\n valor : i.valor,\n loteExterno : i.numeroExternoLote\n}))\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":760,"y":660,"wires":[["1b5d819e.15c73e"]]},{"id":"1b5d819e.15c73e","type":"debug","z":"c74669a0.6a34f8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":910,"y":660,"wires":[]}]

```

---

<div class="post-metadata">

**Author:** ![ndz](https://avatars.discourse-cdn.com/v4/letter/n/a183cd/32.png) [@ndz](https://discourse.nodered.org/u/ndz)\
**Post date:** [17 May 2021 13:37 UTC](https://discourse.nodered.org/t/how-to-repeat-a-flow-based-on-an-array-of-objetcts/45749/8 "2021-05-17T13:37:33Z")

</div>

that actually works fine but then my function doesn't save the data in my context "coletas\_dia", it just saves from the last iten in the array

But i guess i can work from that, thanks a lot for your help!!

---

<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:** [17 May 2021 16:24 UTC](https://discourse.nodered.org/t/how-to-repeat-a-flow-based-on-an-array-of-objetcts/45749/9 "2021-05-17T16:24:21Z")

</div>

save the results to context in array format and push new result to the context

```auto
var results = flow.get("var") || [];
var requestresult = create object of results or string etc;
results.push(requestresults);
flow.set("var", results);
etc

```

something like above

---

<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:** [31 May 2021 16:24 UTC](https://discourse.nodered.org/t/how-to-repeat-a-flow-based-on-an-array-of-objetcts/45749/10 "2021-05-31T16:24:59Z")

</div>

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