# Join values into array in specific order to make a calculation

**URL:** <https://discourse.nodered.org/t/join-values-into-array-in-specific-order-to-make-a-calculation/67774>\
**Category:** General\
**Created:** [15 September 2022 23:11 UTC](https://discourse.nodered.org/t/join-values-into-array-in-specific-order-to-make-a-calculation/67774 "2022-09-15T23:11:06Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![niklerus](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/niklerus/32/67812_2.png) [@niklerus](https://discourse.nodered.org/u/niklerus)\
**Post date:** [15 September 2022 23:11 UTC](https://discourse.nodered.org/t/join-values-into-array-in-specific-order-to-make-a-calculation/67774/1 "2022-09-15T23:11:06Z")

</div>

Trying to make a calculation in NR using Join to create an array and then do a division in a calculator node.  
Everything works as expected if the values (numerator and denominator) arrives in the correct order but they don't always do.  
Setting a delay on one input feels like a bad work around.  
Is there anyway to get them sorted in the join node?  
Been looking a msg.part.index without luck but was not really knowing what I was doing.  
Made a simple flow to illustrate the issue.  
I will get different values depending on what I inject first since I'm doing a division.  
I need it to always sort upper value as numerator and the lower as denominator.

 ![join and calc](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/5/35fc6793c77e92637eaa4cc74d8a1bb1d6b48798.png)

The flow:

```auto
[{"id":"9a11d6ef7f86328c","type":"tab","label":"Flow 1","disabled":false,"info":"","env":[]},{"id":"2e221b1.51df8e4","type":"inject","z":"9a11d6ef7f86328c","name":"numerator","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"0.5","payloadType":"num","x":480,"y":400,"wires":[["9d63e1180abe6daf"]]},{"id":"10bd8f79.53bee9","type":"inject","z":"9a11d6ef7f86328c","name":"denominator","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"1","payloadType":"num","x":490,"y":440,"wires":[["9d63e1180abe6daf"]]},{"id":"9d63e1180abe6daf","type":"join","z":"9a11d6ef7f86328c","name":"","mode":"custom","build":"array","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"","count":"2","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":670,"y":420,"wires":[["884d77d6bc170327"]]},{"id":"884d77d6bc170327","type":"calculator","z":"9a11d6ef7f86328c","name":"","inputMsgField":"payload","outputMsgField":"payload","operation":"div","constant":"","round":false,"decimals":0,"x":810,"y":420,"wires":[["8a73976f9414d377"]]},{"id":"8a73976f9414d377","type":"debug","z":"9a11d6ef7f86328c","name":"","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":970,"y":420,"wires":[]}]

```

---

<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:** [16 September 2022 07:19 UTC](https://discourse.nodered.org/t/join-values-into-array-in-specific-order-to-make-a-calculation/67774/2 "2022-09-16T07:19:49Z")

</div>

Dont use array mode if you want guaranteed order. Use key/value mode.

NOTE: It is always courteous to inlude the full name of any contrib nodes you are using in your demo flow (there are after all 4000+ contrib nodes)

OK, so here is a demo of how you can continue to use the calculator node. It uses the key/value mode of the join node to guarantee values are named and predictable regardless of the order they arrive. Once the payload is set with the values, we convert them to an array using a change node.

The second demo shows how you can avoid this step AND avoid the extra contrib node.

![chrome_3rCN1V7O4i](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/a/6aac8b7e8c2f9b1eafe00426dbd7ce3fc8a01344.gif)

The flow...

```auto
[{"id":"2e221b1.51df8e4","type":"inject","z":"9a11d6ef7f86328c","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"numerator","payload":"3","payloadType":"num","x":1190,"y":100,"wires":[["9d63e1180abe6daf"]]},{"id":"10bd8f79.53bee9","type":"inject","z":"9a11d6ef7f86328c","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"denominator","payload":"10","payloadType":"num","x":1200,"y":140,"wires":[["9d63e1180abe6daf"]]},{"id":"9d63e1180abe6daf","type":"join","z":"9a11d6ef7f86328c","name":"","mode":"custom","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"","count":"2","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":1370,"y":120,"wires":[["d49ab23df184922e"]]},{"id":"8a73976f9414d377","type":"debug","z":"9a11d6ef7f86328c","name":"","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":1630,"y":180,"wires":[]},{"id":"884d77d6bc170327","type":"calculator","z":"9a11d6ef7f86328c","name":"","inputMsgField":"payload","outputMsgField":"payload","operation":"div","constant":"","round":false,"decimals":0,"x":1490,"y":180,"wires":[["8a73976f9414d377"]]},{"id":"d49ab23df184922e","type":"change","z":"9a11d6ef7f86328c","name":"copy num' and den' to array","rules":[{"t":"set","p":"payload","pt":"msg","to":"[payload.numerator, payload.denominator]","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":1560,"y":120,"wires":[["884d77d6bc170327"]]},{"id":"cde63c53c172712c","type":"inject","z":"9a11d6ef7f86328c","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"numerator","payload":"3","payloadType":"num","x":1190,"y":240,"wires":[["41053aaafd46bec0"]]},{"id":"3072bf783ee7514c","type":"inject","z":"9a11d6ef7f86328c","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"denominator","payload":"10","payloadType":"num","x":1200,"y":280,"wires":[["41053aaafd46bec0"]]},{"id":"41053aaafd46bec0","type":"join","z":"9a11d6ef7f86328c","name":"","mode":"custom","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"","count":"2","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":1370,"y":260,"wires":[["ca19b97897c49bc1"]]},{"id":"ca19b97897c49bc1","type":"change","z":"9a11d6ef7f86328c","name":"Divide","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload.numerator / payload.denominator","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":1490,"y":260,"wires":[["289253c8d71ff355"]]},{"id":"289253c8d71ff355","type":"debug","z":"9a11d6ef7f86328c","name":"","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":1630,"y":260,"wires":[]}]

```

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [16 September 2022 08:22 UTC](https://discourse.nodered.org/t/join-values-into-array-in-specific-order-to-make-a-calculation/67774/3 "2022-09-16T08:22:31Z")

</div>

It can also be done just using the join in automatic mode - but you need to specify the parts property correctly - eg for the denominator

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/1/a1a7dffcdbb386dd064d29f7abfeede5f06f6cfd.png)  
and `"index":0` for the numerator.  
(and set the join into automatic mode)

Ideally you would also change the id for each pair to arrive - to identify them as a pair - but you may be able to work ok without doing that.

---

<div class="post-metadata">

**Author:** ![niklerus](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/niklerus/32/67812_2.png) [@niklerus](https://discourse.nodered.org/u/niklerus)\
**Post date:** [16 September 2022 09:18 UTC](https://discourse.nodered.org/t/join-values-into-array-in-specific-order-to-make-a-calculation/67774/4 "2022-09-16T09:18:36Z")

</div>

Thanks @Steve-Mcl and @dceejay !!  
Great info, big thank you to the both of you.  
Learned new, very usable stuff from both your posts.

PS. When creating a post like you did @Steve-Mcl , how do you make the code into single line and get  
the Copy code option when hoovering over the code?  
Will also remember to include full contrib node name..

---

<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:** [16 September 2022 09:32 UTC](https://discourse.nodered.org/t/join-values-into-array-in-specific-order-to-make-a-calculation/67774/5 "2022-09-16T09:32:31Z")

</div>

In order to make code readable and usable it is necessary to surround your code with three backticks (also known as a left quote or backquote `````)

````auto
``` 
   code goes here 
```

````

You can edit and correct your post by clicking the pencil ✏ icon.

See this post for more details - [How to share code or flow json](https://discourse.nodered.org/t/how-to-share-code-or-flow-json/506)

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [16 September 2022 09:57 UTC](https://discourse.nodered.org/t/join-values-into-array-in-specific-order-to-make-a-calculation/67774/6 "2022-09-16T09:57:15Z")

</div>

Also, when you export, go to the JSON tab and check that Compact is selected down at the bottom.

---

<div class="post-metadata">

**Author:** ![niklerus](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/niklerus/32/67812_2.png) [@niklerus](https://discourse.nodered.org/u/niklerus)\
**Post date:** [16 September 2022 10:12 UTC](https://discourse.nodered.org/t/join-values-into-array-in-specific-order-to-make-a-calculation/67774/7 "2022-09-16T10:12:55Z")

</div>

Thanks, I did use the 3 backquotes at start and end but inline with text, corrected it now.

---

<div class="post-metadata">

**Author:** ![niklerus](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/niklerus/32/67812_2.png) [@niklerus](https://discourse.nodered.org/u/niklerus)\
**Post date:** [16 September 2022 12:40 UTC](https://discourse.nodered.org/t/join-values-into-array-in-specific-order-to-make-a-calculation/67774/8 "2022-09-16T12:40:17Z")

</div>

Didn't get your solution to work, got the below output after injecting what you suggested.  
Am I missing something?  
 ![nodes](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/d/5d9624fd467d10bed2b819825460e3fa96c2b361.png)

 ![error](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/e/7e0ed4a147b4524428b9aba9b0e890c2a642c392.png)

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [16 September 2022 14:49 UTC](https://discourse.nodered.org/t/join-values-into-array-in-specific-order-to-make-a-calculation/67774/9 "2022-09-16T14:49:47Z")

</div>

Is ID supposed to be id lowercase ?

---

<div class="post-metadata">

**Author:** ![niklerus](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/niklerus/32/67812_2.png) [@niklerus](https://discourse.nodered.org/u/niklerus)\
**Post date:** [16 September 2022 15:05 UTC](https://discourse.nodered.org/t/join-values-into-array-in-specific-order-to-make-a-calculation/67774/10 "2022-09-16T15:05:12Z")

</div>

You're right,sorry.  
Changed into lowercase.  
No error message from the join node now, but in the Divide node I get;  
 ![1](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/7/97fdcb5430dced7529f63cbb1ff1481e77f541eb.png)

 ![2](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/f/7f825c73fd6945c7845ae63c879b756dfbcc64fe.png)

Also, something must be wrong with the data sent from Join node, debug 8 has payload 10

```auto
[{"id":"9a11d6ef7f86328c","type":"tab","label":"Flow 1","disabled":false,"info":"","env":[]},{"id":"9eac2eb719f072e9","type":"inject","z":"9a11d6ef7f86328c","name":"","props":[{"p":"payload"},{"p":"parts","v":"{\"index\":0,\"id\":\"ABC\",\"count\":2}","vt":"json"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"3","payloadType":"num","x":610,"y":100,"wires":[["28e6cf4fe0402735","c7e7aae177867498"]]},{"id":"5638e52910bf3e2a","type":"inject","z":"9a11d6ef7f86328c","name":"","props":[{"p":"payload"},{"p":"parts","v":"{\"index\":1,\"id\":\"ABC\",\"count\":2}","vt":"json"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"10","payloadType":"num","x":610,"y":220,"wires":[["28e6cf4fe0402735","45b5e9cceb9bd089"]]},{"id":"28e6cf4fe0402735","type":"join","z":"9a11d6ef7f86328c","name":"","mode":"auto","build":"array","property":"parts","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"","count":"2","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":850,"y":180,"wires":[["974b58f461b18778","12832ecf480c3c25"]]},{"id":"5a7d3cf45cde9542","type":"debug","z":"9a11d6ef7f86328c","name":"","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":1290,"y":180,"wires":[]},{"id":"974b58f461b18778","type":"calculator","z":"9a11d6ef7f86328c","name":"Divide","inputMsgField":"payload","outputMsgField":"payload","operation":"div","constant":"","round":false,"decimals":0,"x":1110,"y":180,"wires":[["5a7d3cf45cde9542"]]},{"id":"c7e7aae177867498","type":"debug","z":"9a11d6ef7f86328c","name":"debug 6","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1100,"y":80,"wires":[]},{"id":"45b5e9cceb9bd089","type":"debug","z":"9a11d6ef7f86328c","name":"debug 7","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":972.22216796875,"y":247.77777099609375,"wires":[]},{"id":"12832ecf480c3c25","type":"debug","z":"9a11d6ef7f86328c","name":"debug 8","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"true","targetType":"full","statusVal":"payload","statusType":"auto","x":1020,"y":140,"wires":[]}]

```

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [16 September 2022 15:06 UTC](https://discourse.nodered.org/t/join-values-into-array-in-specific-order-to-make-a-calculation/67774/11 "2022-09-16T15:06:23Z")

</div>

Can you share your latest flow?

---

<div class="post-metadata">

**Author:** ![niklerus](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/niklerus/32/67812_2.png) [@niklerus](https://discourse.nodered.org/u/niklerus)\
**Post date:** [16 September 2022 15:07 UTC](https://discourse.nodered.org/t/join-values-into-array-in-specific-order-to-make-a-calculation/67774/12 "2022-09-16T15:07:20Z")

</div>

Shared it, it's at the bottom.

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [16 September 2022 15:33 UTC](https://discourse.nodered.org/t/join-values-into-array-in-specific-order-to-make-a-calculation/67774/13 "2022-09-16T15:33:23Z")

</div>

Hmm - seems to be a small bug... - if you open the join node and set to manual - you will see it says join all msg.parts... that should be msg.payload... then set it back to automatic...

---

<div class="post-metadata">

**Author:** ![niklerus](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/niklerus/32/67812_2.png) [@niklerus](https://discourse.nodered.org/u/niklerus)\
**Post date:** [16 September 2022 15:39 UTC](https://discourse.nodered.org/t/join-values-into-array-in-specific-order-to-make-a-calculation/67774/14 "2022-09-16T15:39:56Z")

</div>

It works, amazing that you found that!  
Thanks again!

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [16 September 2022 15:42 UTC](https://discourse.nodered.org/t/join-values-into-array-in-specific-order-to-make-a-calculation/67774/15 "2022-09-16T15:42:23Z")

</div>

when you switch to automatic it should reset that hidden field... will have to fix that.

---

<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:** [30 September 2022 15:42 UTC](https://discourse.nodered.org/t/join-values-into-array-in-specific-order-to-make-a-calculation/67774/16 "2022-09-30T15:42:44Z")

</div>

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