# How to concatenate multiple arrays

**URL:** <https://discourse.nodered.org/t/how-to-concatenate-multiple-arrays/23089>\
**Category:** General\
**Created:** [14 March 2020 00:43 UTC](https://discourse.nodered.org/t/how-to-concatenate-multiple-arrays/23089 "2020-03-14T00:43:10Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![rromele](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/rromele/32/18431_2.png) [@rromele](https://discourse.nodered.org/u/rromele)\
**Post date:** [14 March 2020 00:43 UTC](https://discourse.nodered.org/t/how-to-concatenate-multiple-arrays/23089/1 "2020-03-14T00:43:10Z")

</div>

Hi,  
i have 5 nodes that output an array,  
how can i concatenate them into a single array?

_ **example** _ _ **:** _  
node A -\>\> [-8192, 17418]  
node B -\>\> [-13107, 17143]  
node C -\>\> [-15856, 18725]  
node D -\>\> [0, 18725]  
node E -\>\> [0, 16320]

_ **Desired Out:** _  
OUT == [-8192, 17418,-13107, 17143,-15856, 18725,0,18725,0,16320]

**Then I have to send them via the "ModbusFlexiWrite" node:**  
msg.payload = { 'value': [OUT],  
'fc': 16,  
'unitid': 1,  
'address': 30 ,  
'quantity': 5 }

**The result should be this (taking care not to lose negative values)**

![01](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/9/495580c8d52310e921648db5a5b7c4e071294443.jpeg)

 ![02](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/e/1ea80cf5006e2da20e0f7b2f6caa91da2502b907.jpeg)

Thank you so much for your help.  
R.

---

<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:** [14 March 2020 03:00 UTC](https://discourse.nodered.org/t/how-to-concatenate-multiple-arrays/23089/2 "2020-03-14T03:00:29Z")

</div>

One possible way to achieve that is using a join node to accumulate the output of the five nodes and then a function node to concatenate them in a single array. The function node might use the spread syntax for the job:

```auto
let pay = msg.payload;
return {"value" : [...pay[0], ...pay[1], ...pay[2], ...pay[3], ...pay[4]]};

```

Flow:

```auto
[{"id":"9709bc5b.dc8c9","type":"tab","label":"Flow 3","disabled":false,"info":""},{"id":"14f6b95b.3a14c7","type":"inject","z":"9709bc5b.dc8c9","name":"","topic":"","payload":"[-8192, 17418]","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":290,"y":180,"wires":[["404f4a70.9c6de4"]]},{"id":"323110fa.1ee8b","type":"inject","z":"9709bc5b.dc8c9","name":"","topic":"","payload":"[-13107, 17143]","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":300,"y":220,"wires":[["404f4a70.9c6de4"]]},{"id":"a982d18e.638d4","type":"inject","z":"9709bc5b.dc8c9","name":"","topic":"","payload":"[-15856, 18725]","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":300,"y":260,"wires":[["404f4a70.9c6de4"]]},{"id":"292bf366.07d3cc","type":"inject","z":"9709bc5b.dc8c9","name":"","topic":"","payload":"[0, 18725]","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":280,"y":300,"wires":[["404f4a70.9c6de4"]]},{"id":"a3694647.dce098","type":"inject","z":"9709bc5b.dc8c9","name":"","topic":"","payload":"[0, 16320]","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":280,"y":340,"wires":[["404f4a70.9c6de4"]]},{"id":"7afc4c37.a2f9c4","type":"debug","z":"9709bc5b.dc8c9","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":850,"y":280,"wires":[]},{"id":"404f4a70.9c6de4","type":"join","z":"9709bc5b.dc8c9","name":"","mode":"custom","build":"array","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"","count":"5","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":560,"y":280,"wires":[["16ec7547.e7d6eb"]]},{"id":"16ec7547.e7d6eb","type":"function","z":"9709bc5b.dc8c9","name":"concat","func":"let pay = msg.payload;\nreturn {\"value\" : [...pay[0], ...pay[1], ...pay[2], ...pay[3], ...pay[4]]};","outputs":1,"noerr":0,"x":690,"y":280,"wires":[["7afc4c37.a2f9c4"]]}]

```

---

<div class="post-metadata">

**Author:** ![rromele](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/rromele/32/18431_2.png) [@rromele](https://discourse.nodered.org/u/rromele)\
**Post date:** [14 March 2020 22:05 UTC](https://discourse.nodered.org/t/how-to-concatenate-multiple-arrays/23089/3 "2020-03-14T22:05:52Z")

</div>

**Perfect, excellent solution, you are a myth thank you very much for your help!**

Last question, I am “injecting” the result obtained in the "ModbusFlexiWrite" node but I am also wrong and consequently Modbus does not work,

_I get this:_

 ![mdb-errato2](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/0/508c5703a12ee623bbb3b7fafa46b09d483d6fc4.jpeg) ![mdb-errato](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/b/0bfec5d41dba94a6a48f2d34afdee8fb11825aae.jpeg)  
_While I should get this:_  
 ![mdb-giusto](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/2/b229eb6bc6150003fa237765f0bae8167ced11a2.jpeg) ![mdb-giusto2](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/0/b09e3764fdb01cc47c1b1b19667ad644ff669eb7.jpeg)

_Where can I find in-depth manuals on this language and on the node-red base nodes that I consider very powerful (Join, switch, change, etc ...)?_

**Thanks again for the help.**

---

<div class="post-metadata">

**Author:** ![rromele](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/rromele/32/18431_2.png) [@rromele](https://discourse.nodered.org/u/rromele)\
**Post date:** [15 March 2020 01:35 UTC](https://discourse.nodered.org/t/how-to-concatenate-multiple-arrays/23089/4 "2020-03-15T01:35:55Z")

</div>

i am trying this way:

let pay = msg.payload;  
msg.payload = {  
'value':[...pay[0], ...pay[1], ...pay[2], ...pay[3], ...pay[4]],  
'fc': 16,  
'unitid': 1,  
'address': 50,  
'quantity': 10 }  
return msg

I find that the ModbusFlexiWrite node only writes when it receives all 10 registers in positive format, is there any logic in this?

At this point the question I ask myself is: how do you send 5 numbers in floating point with the ModbusFlexiWrite node?

---

<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:** [29 March 2020 01:35 UTC](https://discourse.nodered.org/t/how-to-concatenate-multiple-arrays/23089/5 "2020-03-29T01:35:58Z")

</div>

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