# Data from Multiple Arrays to a single Array

**URL:** <https://discourse.nodered.org/t/data-from-multiple-arrays-to-a-single-array/12635>\
**Category:** General\
**Created:** [26 June 2019 22:25 UTC](https://discourse.nodered.org/t/data-from-multiple-arrays-to-a-single-array/12635 "2019-06-26T22:25:32Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![mjbhuyan](https://avatars.discourse-cdn.com/v4/letter/m/7cd45c/32.png) [@mjbhuyan](https://discourse.nodered.org/u/mjbhuyan)\
**Post date:** [26 June 2019 22:25 UTC](https://discourse.nodered.org/t/data-from-multiple-arrays-to-a-single-array/12635/1 "2019-06-26T22:25:32Z")

</div>

Hi,  
I'm try to merge data from the multiple arrays into a single array, but I'm unable debug the data. Can someone provide any suggestions.

Thank you

Regards

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

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [26 June 2019 22:33 UTC](https://discourse.nodered.org/t/data-from-multiple-arrays-to-a-single-array/12635/2 "2019-06-26T22:33:12Z")

</div>

Hi @mjbhuyan

That screenshot alone does not give us enough information to help.

Can you provide an example of the arrays you have and what you would want the result to look like?

---

<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:** [26 June 2019 22:38 UTC](https://discourse.nodered.org/t/data-from-multiple-arrays-to-a-single-array/12635/3 "2019-06-26T22:38:23Z")

</div>

Mate you need to post your code not just screenshots. We can't copy/edit/paste and I'm certainly not about to re-type from a screenshot.

I suggest you put a real sample data into an inject node connected to your function node with your code in and post that with an example of how you want the data to look and how you want it to appear in the output.

---

<div class="post-metadata">

**Author:** ![mjbhuyan](https://avatars.discourse-cdn.com/v4/letter/m/7cd45c/32.png) [@mjbhuyan](https://discourse.nodered.org/u/mjbhuyan)\
**Post date:** [26 June 2019 22:40 UTC](https://discourse.nodered.org/t/data-from-multiple-arrays-to-a-single-array/12635/4 "2019-06-26T22:40:15Z")

</div>

Thanks for the reply. I'm try to get the output as an array of all "fields1" in (from 1 to 28) in a new Single array. Somewhat like - [48.8 , ..., 45.7, ...]

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [26 June 2019 22:41 UTC](https://discourse.nodered.org/t/data-from-multiple-arrays-to-a-single-array/12635/5 "2019-06-26T22:41:45Z")

</div>

As @Steve-Mcl has said, if you can provide a sample of the input data that would make it much easier. The structure you are starting with is quite complicated and you'd save us a lot of work if you provided real data we could copy.

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [26 June 2019 22:44 UTC](https://discourse.nodered.org/t/data-from-multiple-arrays-to-a-single-array/12635/6 "2019-06-26T22:44:24Z")

</div>

From what I can see, I think if you pass that message through a Change node, configured to set `msg.payload` to the **expression** :

```auto
$.payload.feeds.field1

```

You'll get what you've asked for.

---

<div class="post-metadata">

**Author:** ![mjbhuyan](https://avatars.discourse-cdn.com/v4/letter/m/7cd45c/32.png) [@mjbhuyan](https://discourse.nodered.org/u/mjbhuyan)\
**Post date:** [26 June 2019 22:44 UTC](https://discourse.nodered.org/t/data-from-multiple-arrays-to-a-single-array/12635/7 "2019-06-26T22:44:35Z")

</div>

**Data Source-** [IOT\_MQTT - ThingSpeak IoT](https://thingspeak.com/channels/802840)

**Function code -**

`var pay = msg.payload;`  
`var outputMsgs =[];`  
`for (var n=0; n<28; n++)`  
`{ `  
` var data=Number(pay.feeds[n].field1);`  
` var myVars = data;`  
` outputMsgs.push(myVars);`  
`}`  
`return [outputMsgs];`

**Clipboard -**

> [{"id":"a6132ac7.081118","type":"tab","label":"Flow 3","disabled":false,"info":""},{"id":"3f474fb7.8182d","type":"inject","z":"a6132ac7.081118","name":"","topic":"","payload":"","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":90,"y":80,"wires":[["b5f30778.720628"]]},{"id":"b5f30778.720628","type":"http request","z":"a6132ac7.081118","name":"","method":"GET","ret":"txt","url":"[https://api.thingspeak.com/channels/803134/fields/field1.json?api\_key=ZDQZR93SSI3SA6K9&results=2100","tls":"","x":310,"y":120,"wires":[["856719a7.783b68"]]},{"id":"8fc78409.522328","type":"debug","z":"a6132ac7.081118","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":710,"y":180,"wires":[]},{"id":"856719a7.783b68","type":"json","z":"a6132ac7.081118","name":"","property":"payload","action":"","pretty":false,"x":360,"y":300,"wires":[["a710d00f.f6ac5"]]},{"id":"58f01f1f.3b823","type":"function","z":"a6132ac7.081118","name":"","func":"\nvar](https://api.thingspeak.com/channels/803134/fields/field1.json?api_key=ZDQZR93SSI3SA6K9&results=2100%22,%22tls%22:%22%22,%22x%22:310,%22y%22:120,%22wires%22:%5B%5B%22856719a7.783b68%22%5D%5D%7D,%7B%22id%22:%228fc78409.522328%22,%22type%22:%22debug%22,%22z%22:%22a6132ac7.081118%22,%22name%22:%22%22,%22active%22:true,%22tosidebar%22:true,%22console%22:false,%22tostatus%22:false,%22complete%22:%22false%22,%22x%22:710,%22y%22:180,%22wires%22:%5B%5D%7D,%7B%22id%22:%22856719a7.783b68%22,%22type%22:%22json%22,%22z%22:%22a6132ac7.081118%22,%22name%22:%22%22,%22property%22:%22payload%22,%22action%22:%22%22,%22pretty%22:false,%22x%22:360,%22y%22:300,%22wires%22:%5B%5B%22a710d00f.f6ac5%22%5D%5D%7D,%7B%22id%22:%2258f01f1f.3b823%22,%22type%22:%22function%22,%22z%22:%22a6132ac7.081118%22,%22name%22:%22%22,%22func%22:%22%5Cnvar) pay = msg.payload;\n\nvar outputMsgs =;\n\nfor (var n=0; n\<28; n++)\n{ \n \n var data=(pay.feeds[n].field1);\n \n var myVars = data;\n \n outputMsgs.push(myVars); \n\n}\n\nreturn [outputMsgs];\n\n","outputs":1,"noerr":0,"x":540,"y":160,"wires":[["8fc78409.522328"]]},{"id":"19fa08e.79b33f7","type":"file in","z":"a6132ac7.081118","name":"","filename":"/home/user/Desktop/Humidity","format":"utf8","chunk":false,"sendError":false,"x":780,"y":100,"wires":[]},{"id":"a710d00f.f6ac5","type":"change","z":"a6132ac7.081118","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload.feeds.field1","tot":"env"}],"action":"","property":"","from":"","to":"","reg":false,"x":520,"y":400,"wires":[["58f01f1f.3b823"]]},{"id":"be66668d.bd4588","type":"function","z":"a6132ac7.081118","name":"Humidity","func":"var all=0;\nfor (var n=0; n\<28; n++)\n{ \n var data = Number(msg.payload.feeds[n].field1);\n var all= all+ data;\n}\nmsg.payload = all/28;\nreturn msg;\n","outputs":1,"noerr":0,"x":700,"y":280,"wires":[]}]

---

<div class="post-metadata">

**Author:** ![mjbhuyan](https://avatars.discourse-cdn.com/v4/letter/m/7cd45c/32.png) [@mjbhuyan](https://discourse.nodered.org/u/mjbhuyan)\
**Post date:** [26 June 2019 22:59 UTC](https://discourse.nodered.org/t/data-from-multiple-arrays-to-a-single-array/12635/8 "2019-06-26T22:59:15Z")

</div>

I tried that, but it seems not to be working.

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [26 June 2019 23:01 UTC](https://discourse.nodered.org/t/data-from-multiple-arrays-to-a-single-array/12635/9 "2019-06-26T23:01:16Z")

</div>

Please read [this post about how to properly format flows and code](https://discourse.nodered.org/t/how-to-share-code-or-flow-json/506)

> [@mjbhuyan](#):
>
> I tried that, but it seems not to be working.

That's a shame. I'm sure if you share a bit more detail about what did happen then we might be able to help. Just saying it doesn't work does not give us anything to work with.

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [26 June 2019 23:05 UTC](https://discourse.nodered.org/t/data-from-multiple-arrays-to-a-single-array/12635/10 "2019-06-26T23:05:02Z")

</div>

Took a look at your flow.

You have configured the Change node to set it to an Environment Variable - that is wrong. It should be the **Expression** type. (Make sure you add the `$.` back to the front of the expression when you fix it).

You then have that Change node wired to your Function node that has your attempt at pulling out the values into an array. That won't work because the Change node has _already done it_.

Fix the Change node and remove the Function node.

---

<div class="post-metadata">

**Author:** ![mjbhuyan](https://avatars.discourse-cdn.com/v4/letter/m/7cd45c/32.png) [@mjbhuyan](https://discourse.nodered.org/u/mjbhuyan)\
**Post date:** [26 June 2019 23:22 UTC](https://discourse.nodered.org/t/data-from-multiple-arrays-to-a-single-array/12635/11 "2019-06-26T23:22:54Z")

</div>

Thank you very much @knolleary for your quick help. I'm quite new to this, so I prompt to make silly mistakes. But, your suggestion worked for me to get the desired output.

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