# Loop an array and pass individual elements to next node and come back

**URL:** <https://discourse.nodered.org/t/loop-an-array-and-pass-individual-elements-to-next-node-and-come-back/52677>\
**Category:** General\
**Created:** [24 October 2021 18:59 UTC](https://discourse.nodered.org/t/loop-an-array-and-pass-individual-elements-to-next-node-and-come-back/52677 "2021-10-24T18:59:42Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![sp11](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/sp11/32/49905_2.png) [@sp11](https://discourse.nodered.org/u/sp11)\
**Post date:** [24 October 2021 18:59 UTC](https://discourse.nodered.org/t/loop-an-array-and-pass-individual-elements-to-next-node-and-come-back/52677/1 "2021-10-24T18:59:42Z")

</div>

I have an API call which returns x serials. I want to take each serial and feed it to the next API call to fetch more data. in short i'm trying to accomplish something like this

```auto
arr = [serial1, serial2, serial3...]
for( i=0, i<len(arr), i++)
data = request.get(url, arr[i])
#do something with data here and move on to the next i

```

I used a split node to get all my serial numbers in array form, but only the first serial number is passed on to the next node. my function node is as follows

```auto
msg.payload.forEach(d => {
    serial = d.id;
    msg.payload = serial;
    node.send(msg);
})

```

I'm have just started learning node-red, any direction is much appreciated

---

<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:** [24 October 2021 20:42 UTC](https://discourse.nodered.org/t/loop-an-array-and-pass-individual-elements-to-next-node-and-come-back/52677/2 "2021-10-24T20:42:17Z")

</div>

Here is a demo to get you moving...

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

Demo flow (Import it into your node-red using CTRL+I)

```auto
[{"id":"6bd960618adeaf79","type":"http in","z":"8ff35f2f8018f9c7","name":"","url":"/api1","method":"get","upload":false,"swaggerDoc":"","x":1640,"y":200,"wires":[["4d712c45698356c2"]]},{"id":"69b5c69aee5912f7","type":"http response","z":"8ff35f2f8018f9c7","name":"","statusCode":"","headers":{},"x":1990,"y":200,"wires":[]},{"id":"0fb1db23201d87c3","type":"http in","z":"8ff35f2f8018f9c7","name":"","url":"/api2/:serial","method":"get","upload":false,"swaggerDoc":"","x":1660,"y":240,"wires":[["fadb3847b1eb9bc0"]]},{"id":"a1711f3d2d9e892d","type":"http response","z":"8ff35f2f8018f9c7","name":"","statusCode":"","headers":{},"x":1990,"y":240,"wires":[]},{"id":"4d712c45698356c2","type":"function","z":"8ff35f2f8018f9c7","name":"data","func":"msg.payload = [\n \"abc123\",\n \"def321\",\n \"xyz987\",\n]\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1830,"y":200,"wires":[["69b5c69aee5912f7"]]},{"id":"138c685c77c555c8","type":"http request","z":"8ff35f2f8018f9c7","name":"","method":"GET","ret":"obj","paytoqs":"ignore","url":"http://localhost:1880/api1","tls":"","persist":false,"proxy":"","authType":"","senderr":false,"x":1810,"y":380,"wires":[["5f62c1722e2fe05d"]]},{"id":"772e9b2431953e82","type":"inject","z":"8ff35f2f8018f9c7","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":1660,"y":380,"wires":[["138c685c77c555c8"]]},{"id":"5f62c1722e2fe05d","type":"split","z":"8ff35f2f8018f9c7","name":"","splt":"\\n","spltType":"str","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"","x":1950,"y":380,"wires":[["acfa01919965eee6","0ccc23004fc92386"]]},{"id":"acfa01919965eee6","type":"http request","z":"8ff35f2f8018f9c7","name":"","method":"GET","ret":"obj","paytoqs":"ignore","url":"http://localhost:1880/api2/{{payload}}","tls":"","persist":false,"proxy":"","authType":"","senderr":false,"x":1670,"y":460,"wires":[["f2e1c4c32a4951eb"]]},{"id":"f2e1c4c32a4951eb","type":"join","z":"8ff35f2f8018f9c7","name":"","mode":"auto","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":"false","timeout":"","count":"","reduceRight":false,"x":1840,"y":460,"wires":[["9cae35c0b9d3cc56"]]},{"id":"fadb3847b1eb9bc0","type":"function","z":"8ff35f2f8018f9c7","name":"data","func":"\nlet serial = msg.req.params.serial;\nmsg.payload = {\n serial : serial,\n serialB64: Buffer.from(serial).toString(\"base64\"),\n \"info\": \"Created by API2\"\n}\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1830,"y":240,"wires":[["a1711f3d2d9e892d"]]},{"id":"9cae35c0b9d3cc56","type":"debug","z":"8ff35f2f8018f9c7","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":2130,"y":460,"wires":[]},{"id":"a2c0438ca5a1e46f","type":"comment","z":"8ff35f2f8018f9c7","name":"The API (dummy)","info":"","x":1660,"y":160,"wires":[]},{"id":"777b51053f53c18d","type":"comment","z":"8ff35f2f8018f9c7","name":"Call API1, split the results, call 2nd API join the results","info":"","x":1780,"y":340,"wires":[]},{"id":"0ccc23004fc92386","type":"debug","z":"8ff35f2f8018f9c7","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":2130,"y":380,"wires":[]}]

```

> [@sp11](#):
>
> I'm have just started learning node-red, any direction is much appreciated

I recommend watching this playlist: [Node-RED Essentials](https://www.youtube.com/playlist?list=PLyNBB9VCLmo1hyO-4fIZ08gqFcXBkHy-6). The videos are done by the developers of node-red. They're nice & short and to the point. You will understand a whole lot more in about 1 hour. A small investment for a lot of gain.

---

<div class="post-metadata">

**Author:** ![sp11](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/sp11/32/49905_2.png) [@sp11](https://discourse.nodered.org/u/sp11)\
**Post date:** [26 October 2021 07:40 UTC](https://discourse.nodered.org/t/loop-an-array-and-pass-individual-elements-to-next-node-and-come-back/52677/3 "2021-10-26T07:40:11Z")

</div>

Thank you! the Split and join method works. Also added some delay after split node to rate limit 1 msg per second.

---

<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:** [9 November 2021 07:40 UTC](https://discourse.nodered.org/t/loop-an-array-and-pass-individual-elements-to-next-node-and-come-back/52677/4 "2021-11-09T07:40:17Z")

</div>

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