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

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.

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

msg.payload = msg.payload.v1.toString() + msg.payload.v2.toString() ;
return msg;
1 Like

@Kiwi_gamer01 change the Function code to this:

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.

thanks it works

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:

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:

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

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 :*)

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