# Help return array index

**URL:** <https://discourse.nodered.org/t/help-return-array-index/67834>\
**Category:** General\
**Created:** [17 September 2022 07:06 UTC](https://discourse.nodered.org/t/help-return-array-index/67834 "2022-09-17T07:06:42Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![agermansen](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/agermansen/32/67878_2.png) [@agermansen](https://discourse.nodered.org/u/agermansen)\
**Post date:** [17 September 2022 07:06 UTC](https://discourse.nodered.org/t/help-return-array-index/67834/1 "2022-09-17T07:06:42Z")

</div>

Hey all

I got this fairly simple flow that takes in an array of todays energi prices and returns the minimum and maximum value, this is all well and good but i need to know when the prices are high and low, can anyone help me get the index number out with the value ?

bonus question!  
It doesn't help me a lot to wake up in the morning and see the the cheepest time to starte the washer was 3 hours ago, is there any way to compare the index number to the hour of the day and only look through index numbers equal to og higher ?

Thanks

ps. this is my first question here, if i f-ed up the format pls let me know.

 ![Skærmbillede 2022-09-17 kl. 08.57.52](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/6/f61f8b0bd2d4b0b1e9615ed16d2819776718cba7.png)

 ![Skærmbillede 2022-09-17 kl. 08.58.17](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/a/9aed290779a5c360e6ad25905732c8cdeb266272.png)

```auto
[{"id":"6c44665eec3e570d","type":"api-current-state","z":"ec94b9448b1541aa","name":"Pris","server":"59eab6e.e46dc48","version":3,"outputs":1,"halt_if":"","halt_if_type":"str","halt_if_compare":"is","entity_id":"sensor.energi_data_service","state_type":"str","blockInputOverrides":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"entity"}],"for":"0","forType":"num","forUnits":"minutes","override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":310,"y":320,"wires":[["6d55f8d530d339fc","b5c298755ab028c0"]]},{"id":"239d9afbf19b5ccb","type":"debug","z":"ec94b9448b1541aa","name":"debug 10","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":600,"y":300,"wires":[]},{"id":"6d55f8d530d339fc","type":"function","z":"ec94b9448b1541aa","name":"Max","func":"{const array1 = msg.data.attributes.today\n\n msg.payload = (Math.round(Math.max(...array1)) / 100) + \" Kr/kWh\"\n\nreturn msg;\n}\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":450,"y":300,"wires":[["239d9afbf19b5ccb"]]},{"id":"e79df40960824a3f","type":"debug","z":"ec94b9448b1541aa","name":"debug 11","active":true,"tosidebar":true,"console":true,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":610,"y":340,"wires":[]},{"id":"b5c298755ab028c0","type":"function","z":"ec94b9448b1541aa","name":"Min","func":"{const array1 = msg.data.attributes.today\n\nmsg.payload = (Math.round(Math.min(...array1))/100) + \" Kr/kWh\"\n\nreturn msg;\n}\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":450,"y":340,"wires":[["e79df40960824a3f"]]},{"id":"2628bf3e25567747","type":"inject","z":"ec94b9448b1541aa","name":"","props":[{"p":"time","v":"15","vt":"num"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":150,"y":320,"wires":[["6c44665eec3e570d"]]},{"id":"59eab6e.e46dc48","type":"server","name":"Home Assistant","version":4,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true,"heartbeat":false,"heartbeatInterval":30,"areaSelector":"friendlyName","deviceSelector":"friendlyName","entitySelector":"friendlyName","statusSeparator":"at: ","statusYear":"hidden","statusMonth":"short","statusDay":"numeric","statusHourCycle":"h23","statusTimeFormat":"h:m"}]

```

---

<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:** [17 September 2022 08:49 UTC](https://discourse.nodered.org/t/help-return-array-index/67834/2 "2022-09-17T08:49:56Z")

</div>

Your flow.json is corrupt it needs to be posted between triple backticks  
e.g.  
```  
code  
```  
As to min max and index , you have not given an example of the array, so i can only give you an example of a simple array, and how to search for min /max and their index.

```auto
[{"id":"1afec0d9.9b7c4f","type":"inject","z":"bf9e1e33.030598","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[5,4,3,2,1,6,7,9,8]","payloadType":"json","x":160,"y":3700,"wires":[["fff21b01.f17078"]]},{"id":"fff21b01.f17078","type":"function","z":"bf9e1e33.030598","name":"","func":"let min = {value: msg.payload[0], index:0};\nlet max = {value: msg.payload[0], index:0};\nmsg.payload.forEach((int, index) =>{\n if(int > max.value) max = {value: int, index: index};\n if(int < min.value) min = {value: int, index: index}\n})\nmsg.payload = {min, max}\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":360,"y":3700,"wires":[["53281e53.747e4"]]},{"id":"53281e53.747e4","type":"debug","z":"bf9e1e33.030598","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":540,"y":3700,"wires":[]}]

```

```auto
let min = {value: msg.payload[0], index:0};
let max = {value: msg.payload[0], index:0};
msg.payload.forEach((int, index) =>{
    if(int > max.value) max = {value: int, index: index};
    if(int < min.value) min = {value: int, index: index};
})
msg.payload = {min, max};
return msg;

```

---

<div class="post-metadata">

**Author:** ![agermansen](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/agermansen/32/67878_2.png) [@agermansen](https://discourse.nodered.org/u/agermansen)\
**Post date:** [17 September 2022 18:24 UTC](https://discourse.nodered.org/t/help-return-array-index/67834/3 "2022-09-17T18:24:36Z")

</div>

thanks a bunch 😃

with just the payload changed to data.attributes.today i got exactly what i wanted 👌

an example of the array could just be this, the index numbers are hours of the day and the value is the price of a kWh

![Skærmbillede 2022-09-17 kl. 20.19.49](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/d/5da45987801cd5215488e104313f42aaf66f0016.png)

have you got any ideas on getting the index number compared to the current hour so i can get the upcomming cheepest price ?

---

<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:** [17 September 2022 18:36 UTC](https://discourse.nodered.org/t/help-return-array-index/67834/4 "2022-09-17T18:36:51Z")

</div>

Then get the hour of day and only look at indices above or equal to the hour.

```auto
const hour = new Date().getHours()
let min = {value: msg.payload[hour], index:hour};
let max = {value: msg.payload[hour], index:hour};
msg.payload.forEach((int, index) =>{
    if(index >= hour){
        if(int > max.value) max = {value: int, index: index};
        else if(int < min.value) min = {value: int, index: index}
    }
})
msg.payload = {min, max}
return msg;

```

Or to minimise the loop and start search at hour index ( not that this small loop really matters)

```auto
let search_array = msg.payload;
let index = new Date().getHours();
let min = {value: search_array[index], index: index};
let max = {value: search_array[index], index: index};
for(index; index < 24; index++){
    let int = search_array[index];
    if(int > max.value) max = {value: int, index: index};
    else if(int < min.value) min = {value: int, index: index}
}
msg.payload = {min, max}
return msg;

```

---

<div class="post-metadata">

**Author:** ![agermansen](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/agermansen/32/67878_2.png) [@agermansen](https://discourse.nodered.org/u/agermansen)\
**Post date:** [17 September 2022 19:13 UTC](https://discourse.nodered.org/t/help-return-array-index/67834/5 "2022-09-17T19:13:13Z")

</div>

everything is working now, thanks a lot 😃

---

<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:** [1 October 2022 19:13 UTC](https://discourse.nodered.org/t/help-return-array-index/67834/6 "2022-10-01T19:13:15Z")

</div>

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