# Find smallest and greatest value

**URL:** https://discourse.nodered.org/t/find-smallest-and-greatest-value/97879
**Category:** General
**Created:** [30 June 2025 16:05 UTC](https://discourse.nodered.org/t/find-smallest-and-greatest-value/97879 "2025-06-30T16:05:41Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Janvi](https://avatars.discourse-cdn.com/v4/letter/j/a587f6/32.png) [@Janvi](https://discourse.nodered.org/u/Janvi)
#### Post date: [30 June 2025 16:05 UTC](https://discourse.nodered.org/t/find-smallest-and-greatest-value/97879/1 "2025-06-30T16:05:41Z")

</div>

Assume a JSON like

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/d/ad69b816ef3d7e76553c95d0b6537ba8785ec06c.png)

where it is easy to build a sum by

```auto
msg = {"payload" : msg.payload["AC Out L1"]
                    + msg.payload["AC Out L2"]
                    + msg.payload["AC Out L3"]};
return msg;

```

Now I like to calculate the difference between greatest and smallest value. Like any real measurements, values might change to any size. For 3 elements I need to have 5 if instructions in 3 ident levels for 6 cases:

case1 = L1-L2  
case2 = L1-L3  
case3 = L2-L3  
case4 = L2-L1  
case5 = L3-L2  
case6 = L3-L1

For more elements, probably anything like a bubblesort loop is required. Is there anything on NodeReds default palette what can solve this matter? Application is my 3 phase Diesel genset what ideally should generate power for symmetric loads.

---

<div class="post-metadata">

### Author: ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)
#### Post date: [30 June 2025 16:22 UTC](https://discourse.nodered.org/t/find-smallest-and-greatest-value/97879/2 "2025-06-30T16:22:22Z")

</div>

You can simple create an array of the values and sort their order form low to high, then just take the last array position and subtract the first array position. This should give you the difference between highest and lowest value.

```auto
const sorted_array = Object.values(msg.payload).sort((a, b) => a - b);
msg.payload = sorted_array[sorted_array.length - 1] - sorted_array[0];
return msg;

```

---

<div class="post-metadata">

### Author: ![dynamicdave](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dynamicdave/32/96_2.png) [@dynamicdave](https://discourse.nodered.org/u/dynamicdave)
#### Post date: [30 June 2025 16:23 UTC](https://discourse.nodered.org/t/find-smallest-and-greatest-value/97879/3 "2025-06-30T16:23:24Z")

</div>

Very quick idea for you to try out.  
Note: I've added the difference to the original payload.

 ![monday_difference_A](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/a/5a53a83e8cf4255226c0bf6ef556a1308fed6469.png)  
 ![monday_difference_B](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/3/232d52b3cd4ff9663e6a7afda175b66eacc0e006.png)

Node-RED json flow...

```auto
[{"id":"869324438973fbe4","type":"tab","label":"Difference","disabled":false,"info":"","env":[]},{"id":"c1a8b1d6a2a5a0cd","type":"inject","z":"869324438973fbe4","name":"Sample Input","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"AC_OUT_L1\":1227,\"AC_OUT_L2\":535,\"AC_OUT_L3\":592}","payloadType":"json","x":210,"y":140,"wires":[["de5d197d2c2594b6"]]},{"id":"de5d197d2c2594b6","type":"function","z":"869324438973fbe4","name":"Calculate Max-Min Difference","func":"let values = Object.values(msg.payload);\nlet max = Math.max(...values);\nlet min = Math.min(...values);\n\n// Option 1: Send just the difference\n// msg.payload = max - min;\n\n// Option 2: Add 'difference' to original object\nmsg.payload.difference = max - min;\n\nreturn msg;","outputs":1,"timeout":"","noerr":0,"initialize":"","finalize":"","libs":[],"x":430,"y":140,"wires":[["94f3373de6c20137"]]},{"id":"94f3373de6c20137","type":"debug","z":"869324438973fbe4","name":"Show Result","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":660,"y":140,"wires":[]}]

```

---

<div class="post-metadata">

### Author: ![Janvi](https://avatars.discourse-cdn.com/v4/letter/j/a587f6/32.png) [@Janvi](https://discourse.nodered.org/u/Janvi)
#### Post date: [30 June 2025 16:42 UTC](https://discourse.nodered.org/t/find-smallest-and-greatest-value/97879/4 "2025-06-30T16:42:29Z")

</div>

Many thanks for both solutions. I am going to try. Again, its the power of Javascript, not only NodeRed. The project advances nice and the percentage of asymmetry is already in the dashboard (Schieflast).

Unfortunately, the float number is much too long. I really need to buy a Javascript book. Is there anything like printf to Google for?

 ![Screenshot from 2025-06-30 19-14-30](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/e/be3ac254c69bf841f56af5c1e099f989e9ed472d.png)

Edit: Found the small sister of Math.min and Math.max what is Math.round - perfect. The Math.methods can do really a lot of things.

---

<div class="post-metadata">

### Author: ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)
#### Post date: [30 June 2025 18:36 UTC](https://discourse.nodered.org/t/find-smallest-and-greatest-value/97879/5 "2025-06-30T18:36:43Z")

</div>

> [@Janvi](#):
>
> I really need to buy a Javascript book.

The definitive JS resource [Number.prototype.toFixed() - JavaScript | MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toFixed)  
Also many online tutorials [learn javascript at DuckDuckGo](https://duckduckgo.com/?q=learn+javascript&ia=web)

---

<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: [14 July 2025 18:38 UTC](https://discourse.nodered.org/t/find-smallest-and-greatest-value/97879/6 "2025-07-14T18:38:10Z")

</div>

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