# Openweathermap - how to compare the values inside an array

**URL:** https://discourse.nodered.org/t/openweathermap-how-to-compare-the-values-inside-an-array/45114
**Category:** General
**Created:** [3 May 2021 13:37 UTC](https://discourse.nodered.org/t/openweathermap-how-to-compare-the-values-inside-an-array/45114 "2021-05-03T13:37:21Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![sfedo](https://avatars.discourse-cdn.com/v4/letter/s/3da27b/32.png) [@sfedo](https://discourse.nodered.org/u/sfedo)
#### Post date: [3 May 2021 13:37 UTC](https://discourse.nodered.org/t/openweathermap-how-to-compare-the-values-inside-an-array/45114/1 "2021-05-03T13:37:21Z")

</div>

Hello guys,  
I'm using openweathermap node and get 5 day data.  
Now I'm only interested in clouds object inside the array. Is there a way to extract the lowest value?

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/2/e23c94ad0491fd891b381f06696ad1af4ac54eac.png)

in the picture you can see the first value of 40  
Now I need a function to search for clouds.all value in all 40 objects and pick the lowest one to return it.

---

<div class="post-metadata">

### Author: ![UnborN](https://avatars.discourse-cdn.com/v4/letter/u/4491bb/32.png) [@UnborN](https://discourse.nodered.org/u/UnborN)
#### Post date: [3 May 2021 14:22 UTC](https://discourse.nodered.org/t/openweathermap-how-to-compare-the-values-inside-an-array/45114/2 "2021-05-03T14:22:03Z")

</div>

Hi sfedo,

> [@sfedo](#):
>
> Is there a way to extract the lowest value?

Thats were Jsonata expressions shine, with very little code  
In a Change node try `$min(msg.payload.clouds.all)`

**Example Flow:**

```auto
[{"id":"fe803e82.c6981","type":"inject","z":"8889c53d.0c1208","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[{\"dt\":1620054000,\"main\":{},\"weather\":[],\"clouds\":{\"all\":83},\"wind\":{},\"visibility\":10000,\"pop\":0.97,\"rain\":{},\"sys\":{},\"dt_txt\":\"2021-05-03 15:00:00\"},{\"dt\":1620054200,\"main\":{},\"weather\":[],\"clouds\":{\"all\":23},\"wind\":{},\"visibility\":10000,\"pop\":0.97,\"rain\":{},\"sys\":{},\"dt_txt\":\"2021-05-03 15:00:00\"},{\"dt\":1620054400,\"main\":{},\"weather\":[],\"clouds\":{\"all\":33},\"wind\":{},\"visibility\":10000,\"pop\":0.97,\"rain\":{},\"sys\":{},\"dt_txt\":\"2021-05-03 15:00:00\"},{\"dt\":1620054500,\"main\":{},\"weather\":[],\"clouds\":{\"all\":55},\"wind\":{},\"visibility\":10000,\"pop\":0.97,\"rain\":{},\"sys\":{},\"dt_txt\":\"2021-05-03 15:00:00\"},{\"dt\":1620064000,\"main\":{},\"weather\":[],\"clouds\":{\"all\":73},\"wind\":{},\"visibility\":10000,\"pop\":0.97,\"rain\":{},\"sys\":{},\"dt_txt\":\"2021-05-03 15:00:00\"}]","payloadType":"json","x":300,"y":340,"wires":[["94b1e917.13e86","e2dec559.5440b"]]},{"id":"94b1e917.13e86","type":"change","z":"8889c53d.0c1208","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"$min(msg.payload.clouds.all)\t","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":510,"y":340,"wires":[["23c1a312.0a086c"]]},{"id":"e2dec559.5440b","type":"debug","z":"8889c53d.0c1208","name":"1","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":370,"y":260,"wires":[]},{"id":"23c1a312.0a086c","type":"debug","z":"8889c53d.0c1208","name":"2","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":710,"y":340,"wires":[]}]

```

PS. you want the value returned or the whole object that has the lowest cloud.all value?

---

<div class="post-metadata">

### Author: ![sfedo](https://avatars.discourse-cdn.com/v4/letter/s/3da27b/32.png) [@sfedo](https://discourse.nodered.org/u/sfedo)
#### Post date: [3 May 2021 14:40 UTC](https://discourse.nodered.org/t/openweathermap-how-to-compare-the-values-inside-an-array/45114/3 "2021-05-03T14:40:46Z")

</div>

wow thats really cool, now I would like to have the index of this value? 🙂

---

<div class="post-metadata">

### Author: ![UnborN](https://avatars.discourse-cdn.com/v4/letter/u/4491bb/32.png) [@UnborN](https://discourse.nodered.org/u/UnborN)
#### Post date: [3 May 2021 14:59 UTC](https://discourse.nodered.org/t/openweathermap-how-to-compare-the-values-inside-an-array/45114/4 "2021-05-03T14:59:16Z")

</div>

> [@sfedo](#):
>
> now I would like to have the index of this value

hehe .. and thats were i start having problems with Jsonata and default to javascript 😅

```auto
[{"id":"fe803e82.c6981","type":"inject","z":"8889c53d.0c1208","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[{\"dt\":1620054000,\"main\":{},\"weather\":[],\"clouds\":{\"all\":83},\"wind\":{},\"visibility\":10000,\"pop\":0.97,\"rain\":{},\"sys\":{},\"dt_txt\":\"2021-05-03 15:00:00\"},{\"dt\":1620054200,\"main\":{},\"weather\":[],\"clouds\":{\"all\":23},\"wind\":{},\"visibility\":10000,\"pop\":0.97,\"rain\":{},\"sys\":{},\"dt_txt\":\"2021-05-03 15:00:00\"},{\"dt\":1620054400,\"main\":{},\"weather\":[],\"clouds\":{\"all\":33},\"wind\":{},\"visibility\":10000,\"pop\":0.97,\"rain\":{},\"sys\":{},\"dt_txt\":\"2021-05-03 15:00:00\"},{\"dt\":1620054500,\"main\":{},\"weather\":[],\"clouds\":{\"all\":55},\"wind\":{},\"visibility\":10000,\"pop\":0.97,\"rain\":{},\"sys\":{},\"dt_txt\":\"2021-05-03 15:00:00\"},{\"dt\":1620064000,\"main\":{},\"weather\":[],\"clouds\":{\"all\":73},\"wind\":{},\"visibility\":10000,\"pop\":0.97,\"rain\":{},\"sys\":{},\"dt_txt\":\"2021-05-03 15:00:00\"}]","payloadType":"json","x":300,"y":340,"wires":[["e2dec559.5440b","1497fe3c.30f472"]]},{"id":"e2dec559.5440b","type":"debug","z":"8889c53d.0c1208","name":"1","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":370,"y":260,"wires":[]},{"id":"1497fe3c.30f472","type":"function","z":"8889c53d.0c1208","name":"","func":"let minValue = msg.payload[0].clouds.all // start with 1st value\nlet minIndex = 0\n\nmsg.payload.forEach( (el, index) => {\n \n if (el.clouds.all < minValue ) {\n minValue = el.clouds.all\n minIndex = index\n }\n})\n\nmsg.minValue = minValue\nmsg.minIndex = minIndex\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":500,"y":340,"wires":[["979e8a95.db4678"]]},{"id":"979e8a95.db4678","type":"debug","z":"8889c53d.0c1208","name":"2","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":680,"y":340,"wires":[]}]

```

---

<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: [3 May 2021 16:47 UTC](https://discourse.nodered.org/t/openweathermap-how-to-compare-the-values-inside-an-array/45114/5 "2021-05-03T16:47:05Z")

</div>

```auto
($lowest := $min($.payload.clouds.all);
$map(
   payload,
   function ($v,$i){
       $v.clouds.all = $lowest ? $i
   }
))

```

would return the index.  
But would it not be best to return the whole object that has the lowest cloud.all

```auto
($lowest := $min($.payload.clouds.all);	
payload[$.clouds.all = $lowest])	

```

---

<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: [2 July 2021 16:47 UTC](https://discourse.nodered.org/t/openweathermap-how-to-compare-the-values-inside-an-array/45114/6 "2021-07-02T16:47:34Z")

</div>

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