# Send each item of array in a message payload separately

**URL:** <https://discourse.nodered.org/t/send-each-item-of-array-in-a-message-payload-separately/10414>\
**Category:** General\
**Created:** [22 April 2019 01:32 UTC](https://discourse.nodered.org/t/send-each-item-of-array-in-a-message-payload-separately/10414 "2019-04-22T01:32:42Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![hathemi](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hathemi/32/3792_2.png) [@hathemi](https://discourse.nodered.org/u/hathemi)\
**Post date:** [22 April 2019 01:32 UTC](https://discourse.nodered.org/t/send-each-item-of-array-in-a-message-payload-separately/10414/1 "2019-04-22T01:32:42Z")

</div>

I have an array msg.options =[1,2,3,4,5], and i want to send each item in msg.payload.  
i used this in function

```auto
for( var i = 0; i < msg.options.length; i++){ 
   msg.payload = msg.options[i]
    return msg;
}

```

But it always send me the first item. msg.options[0].  
Any ideas will be appreciated.

---

<div class="post-metadata">

**Author:** ![Andrei](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/andrei/32/10446_2.png) [@Andrei](https://discourse.nodered.org/u/Andrei)\
**Post date:** [22 April 2019 01:50 UTC](https://discourse.nodered.org/t/send-each-item-of-array-in-a-message-payload-separately/10414/2 "2019-04-22T01:50:20Z")

</div>

You will find a code ready to use in the docs. See [Multiple Messages](https://nodered.org/docs/writing-functions#multiple-messages):

I would just change the loop in the function as below (personal preference):

```auto
let SplitMsg=[];

for (let elem of msg.payload) {
    SplitMsg.push({payload:elem});
}

return [SplitMsg];

```

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [22 April 2019 06:38 UTC](https://discourse.nodered.org/t/send-each-item-of-array-in-a-message-payload-separately/10414/3 "2019-04-22T06:38:30Z")

</div>

Of just use the split node

---

<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:** [22 April 2019 08:20 UTC](https://discourse.nodered.org/t/send-each-item-of-array-in-a-message-payload-separately/10414/4 "2019-04-22T08:20:05Z")

</div>

FYI, your code was almost perfect, if you had used node.send...

```auto
for( var i = 0; i < msg.options.length; i++){ 
   msg.payload = msg.options[i]
   node.send(msg);
}

```

Nice to have options (pun intended)

---

<div class="post-metadata">

**Author:** ![hathemi](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hathemi/32/3792_2.png) [@hathemi](https://discourse.nodered.org/u/hathemi)\
**Post date:** [22 April 2019 08:27 UTC](https://discourse.nodered.org/t/send-each-item-of-array-in-a-message-payload-separately/10414/5 "2019-04-22T08:27:14Z")

</div>

> [@Steve-Mcl](#):
>
> node.send(msg);

good to know about" node.send". i never used it before. Thank you all.

---

<div class="post-metadata">

**Author:** ![ukmoose](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ukmoose/32/13_2.png) [@ukmoose](https://discourse.nodered.org/u/ukmoose)\
**Post date:** [22 April 2019 08:34 UTC](https://discourse.nodered.org/t/send-each-item-of-array-in-a-message-payload-separately/10414/6 "2019-04-22T08:34:00Z")

</div>

It might be worth re-reading the docs...  
[https://nodered.org/docs/writing-functions#sending-messages-asynchronously](https://nodered.org/docs/writing-functions#sending-messages-asynchronously)
