# Merge eigth numbers in a function node | same order, everytime

**URL:** <https://discourse.nodered.org/t/merge-eigth-numbers-in-a-function-node-same-order-everytime/26051>\
**Category:** General\
**Created:** [4 May 2020 09:59 UTC](https://discourse.nodered.org/t/merge-eigth-numbers-in-a-function-node-same-order-everytime/26051 "2020-05-04T09:59:05Z")\
**Posts on this page:** 7\
**Page:** 2

<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:** [4 May 2020 12:56 UTC](https://discourse.nodered.org/t/merge-eigth-numbers-in-a-function-node-same-order-everytime/26051/23 "2020-05-04T12:56:51Z")

</div>

possibly. but when I deleted that extra hidden line, it worked perectly.

it took 2 different messages, 1 with topic "v1" and the other with "v2", then in your function you added them together & returned the message.

once I removed the additional wire - it worked.

---

<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:** [4 May 2020 12:57 UTC](https://discourse.nodered.org/t/merge-eigth-numbers-in-a-function-node-same-order-everytime/26051/24 "2020-05-04T12:57:48Z")

</div>

well in that case, your function should be...

```auto
msg.payload = msg.payload.v1.toString() + msg.payload.v2.toString() ;
return msg;

```

---

<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:** [4 May 2020 12:58 UTC](https://discourse.nodered.org/t/merge-eigth-numbers-in-a-function-node-same-order-everytime/26051/25 "2020-05-04T12:58:05Z")

</div>

@Kiwi_gamer01 change the Function code to this:

```auto
msg.payload = ""+msg.payload.v1 + msg.payload.v2;
return msg;

```

The problem that I forgot to account for is the values are numbers... so we need to use the `""+` at the start of the line to force it to treat all the values as strings and to concatenate them, not add them.

---

<div class="post-metadata">

**Author:** ![Kiwi\_gamer01](https://avatars.discourse-cdn.com/v4/letter/k/a587f6/32.png) [@Kiwi\_gamer01](https://discourse.nodered.org/u/Kiwi_gamer01)\
**Post date:** [4 May 2020 12:59 UTC](https://discourse.nodered.org/t/merge-eigth-numbers-in-a-function-node-same-order-everytime/26051/26 "2020-05-04T12:59:39Z")

</div>

thanks it works

---

<div class="post-metadata">

**Author:** ![shrickus](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/shrickus/32/517_2.png) [@shrickus](https://discourse.nodered.org/u/shrickus)\
**Post date:** [4 May 2020 17:20 UTC](https://discourse.nodered.org/t/merge-eigth-numbers-in-a-function-node-same-order-everytime/26051/27 "2020-05-04T17:20:52Z")

</div>

I believe the `join` node can output an array of the 8 numbers, always in the same order -- then you can just use the following Javascript function code to combine them into a string, like so:

```auto
msg.payload = msg.payload.join("");
return msg;

```

The only thing I'm not sure about is whether every position in the array will have a default value, or be undefined before it gets a value (since they arrive in random order) -- so if the value for channel 3 happens to be a JS `undefined`, it will be dropped from the joined string:

```auto
> [1,2,undefined,4].join("")
'124'

```

---

<div class="post-metadata">

**Author:** ![shrickus](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/shrickus/32/517_2.png) [@shrickus](https://discourse.nodered.org/u/shrickus)\
**Post date:** [4 May 2020 17:46 UTC](https://discourse.nodered.org/t/merge-eigth-numbers-in-a-function-node-same-order-everytime/26051/28 "2020-05-04T17:46:38Z")

</div>

> [@shrickus](#):
>
> I believe the `join` node can output an array of the 8 numbers, always in the same order

Hmm, now that I think about it, you may need to combine the 8 values into an object (using the topic as the key), and THEN you can convert it to an array and join it... (e.g. using a JSONata expression)  
`($spread(payload)^($keys($)[0]).*).$string()~>$join("")`

in which case writing out the string manipulations would be simpler to read :\*)

---

<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:** [18 May 2020 17:46 UTC](https://discourse.nodered.org/t/merge-eigth-numbers-in-a-function-node-same-order-everytime/26051/29 "2020-05-18T17:46:42Z")

</div>

Closed on 18 May 2020 17:46 UTC

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

[Previous page](https://discourse.nodered.org/t/merge-eigth-numbers-in-a-function-node-same-order-everytime/26051.md?page=1)
