# Need help combining two msg with arrays

**URL:** <https://discourse.nodered.org/t/need-help-combining-two-msg-with-arrays/63620>\
**Category:** General\
**Created:** [8 June 2022 13:34 UTC](https://discourse.nodered.org/t/need-help-combining-two-msg-with-arrays/63620 "2022-06-08T13:34:21Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![network\_potato](https://avatars.discourse-cdn.com/v4/letter/n/59ef9b/32.png) [@network\_potato](https://discourse.nodered.org/u/network_potato)\
**Post date:** [8 June 2022 13:34 UTC](https://discourse.nodered.org/t/need-help-combining-two-msg-with-arrays/63620/1 "2022-06-08T13:34:22Z")

</div>

I have two messages that each have an array that is called the same (`line_items`).

![122233](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/5/55229baf07c6d5828b3545af2698b930b8889e73.jpeg)

I need to combine them so that I get one message, with an array called `line_items`, that has both of the objects inside it.

I have tried using the join node in manual mode with different type of settings, but the closest I can get is this, where the arrays are placed inside a new array:  
 ![122233](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/5/55229baf07c6d5828b3545af2698b930b8889e73.jpeg)

But I need the objects to be in the array directly (in the "root", if that makes sense?)  
Like this (photoshopped):

![mockup](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/3/83bbe50524e269724862675498d71aa4c04a7d71.jpeg)

---

<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:** [8 June 2022 13:51 UTC](https://discourse.nodered.org/t/need-help-combining-two-msg-with-arrays/63620/2 "2022-06-08T13:51:00Z")

</div>

How do you differentiate them?

Do they come from the same node wire or separate wires?

show a picture of the flow where the messages come from

Without this info, we would be guessing.

---

<div class="post-metadata">

**Author:** ![Ithoh](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ithoh/32/40000_2.png) [@Ithoh](https://discourse.nodered.org/u/Ithoh)\
**Post date:** [8 June 2022 14:05 UTC](https://discourse.nodered.org/t/need-help-combining-two-msg-with-arrays/63620/4 "2022-06-08T14:05:26Z")

</div>

Hello,

Assuming you have only two items, you can come up with something like this using a function node, It might not be the best way to do it, but you can give it a try I guess ! 🙂

(For my method to work you'll need to have differents topics name on your messages to be able to differentiate them, and also I deleted my previous post cause I made a mistake)

```auto
[{"id":"a877e7f86dc43baa","type":"inject","z":"4649623030b5e549","name":"","props":[{"p":"payload.line_items","v":"[{\"item1\":\"item1\"}]","vt":"json"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"item1","x":310,"y":1000,"wires":[["96c64f3e38f8254a"]]},{"id":"f0ad6519098139b0","type":"inject","z":"4649623030b5e549","name":"","props":[{"p":"payload.line_items","v":"[{\"item2\":\"item2\"}]","vt":"json"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"item2","x":310,"y":1060,"wires":[["96c64f3e38f8254a"]]},{"id":"5416325db8307a6f","type":"debug","z":"4649623030b5e549","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":710,"y":1040,"wires":[]},{"id":"96c64f3e38f8254a","type":"function","z":"4649623030b5e549","name":"","func":"var local_msg = RED.util.cloneMessage(msg);\nflow.set(local_msg.topic, local_msg);\n\nvar item1 = flow.get(\"item1\") || \"\";\nvar item2 = flow.get(\"item2\") || \"\";\n\nvar new_item = {};\nnew_item.payload = {};\nnew_item.payload.line_items = [];\n\nif(item1.topic === \"item1\" && item2.topic === \"item2\"){\n new_item.payload.line_items.push(item1.payload.line_items[0])\n new_item.payload.line_items.push(item2.payload.line_items[0])\n\n return new_item;\n}\n\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":520,"y":1040,"wires":[["5416325db8307a6f"]]}]

```

---

<div class="post-metadata">

**Author:** ![network\_potato](https://avatars.discourse-cdn.com/v4/letter/n/59ef9b/32.png) [@network\_potato](https://discourse.nodered.org/u/network_potato)\
**Post date:** [19 June 2022 10:58 UTC](https://discourse.nodered.org/t/need-help-combining-two-msg-with-arrays/63620/5 "2022-06-19T10:58:17Z")

</div>

Thank you! This works perfectly if both item1 and item2 exist.

However, in some cases, only a message with topic of item1 exists. In such cases, I want the message (item1) to be passed through (right now, the function node waits for item2 to arrive, which it never will).

Is that possible to do? 😃

Like this:

 ![mesg](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/f/1fe402ffcd39d3729e94e9b508449a2a54f5cfd2.jpeg)

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [19 June 2022 11:29 UTC](https://discourse.nodered.org/t/need-help-combining-two-msg-with-arrays/63620/6 "2022-06-19T11:29:21Z")

</div>

What will determine if msg2 is not gong to show up?

---

<div class="post-metadata">

**Author:** ![network\_potato](https://avatars.discourse-cdn.com/v4/letter/n/59ef9b/32.png) [@network\_potato](https://discourse.nodered.org/u/network_potato)\
**Post date:** [19 June 2022 16:45 UTC](https://discourse.nodered.org/t/need-help-combining-two-msg-with-arrays/63620/7 "2022-06-19T16:45:37Z")

</div>

I do a check earlier in the flow where a change node looks for a certain item in the message, if the value of that item is greater than 0, the message passes on.

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [19 June 2022 17:05 UTC](https://discourse.nodered.org/t/need-help-combining-two-msg-with-arrays/63620/8 "2022-06-19T17:05:49Z")

</div>

Then I would put a switch node in to test that condition and bypass the join node if it was true otherwise go to the join node.

---

<div class="post-metadata">

**Author:** ![network\_potato](https://avatars.discourse-cdn.com/v4/letter/n/59ef9b/32.png) [@network\_potato](https://discourse.nodered.org/u/network_potato)\
**Post date:** [25 June 2022 11:06 UTC](https://discourse.nodered.org/t/need-help-combining-two-msg-with-arrays/63620/9 "2022-06-25T11:06:36Z")

</div>

Thank you for the solution, it works.

---

<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 July 2022 11:07 UTC](https://discourse.nodered.org/t/need-help-combining-two-msg-with-arrays/63620/10 "2022-07-09T11:07:03Z")

</div>

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