# Loop through an array and return index of a specific value

**URL:** <https://discourse.nodered.org/t/loop-through-an-array-and-return-index-of-a-specific-value/47063>\
**Category:** General\
**Created:** [12 June 2021 10:20 UTC](https://discourse.nodered.org/t/loop-through-an-array-and-return-index-of-a-specific-value/47063 "2021-06-12T10:20:41Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![craigcurtin](https://avatars.discourse-cdn.com/v4/letter/c/94ad74/32.png) [@craigcurtin](https://discourse.nodered.org/u/craigcurtin)\
**Post date:** [12 June 2021 10:20 UTC](https://discourse.nodered.org/t/loop-through-an-array-and-return-index-of-a-specific-value/47063/1 "2021-06-12T10:20:41Z")

</div>

Hey Guys,

I have a flow that does a query on my energy provider and this returns an array with 120 objects in it (1/2 hour increments of pricing for electricity)

The first x (arbitrary number) of these are in the past and are differentiated by a date and time, but more easily by a value in a field of ACTUAL, whereas anything in the future is designated with FORECAST in the same field

What i would like to do is to loop through the array until i find the first object that is a FORECAST and then return the next 20 elements in the array.

Is there a way to do this with standard nodes before i sit down and try to write a function to do this ?

Here is the current flow which can be run and returns the results.

```auto
[{"id":"6028717e.571d7","type":"inject","z":"88029ce.abbab6","name":"Check Prices","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":"300","topic":"","payload":"","payloadType":"date","x":230,"y":500,"wires":[["416043a5.94179c"]]},{"id":"416043a5.94179c","type":"function","z":"88029ce.abbab6","name":"set headers and change Postcode here","func":"msg.headers = {};\nmsg.headers[\"Content-Type\"] = \"application/json\";\nmsg.payload = '{ \"postcode\":\"2069\" }';\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":400,"y":440,"wires":[["a2b6857.bc69078"]]},{"id":"a2b6857.bc69078","type":"http request","z":"88029ce.abbab6","name":"","method":"POST","ret":"obj","paytoqs":"ignore","url":"https://api.amberelectric.com.au/prices/listprices","tls":"","persist":false,"proxy":"","authType":"","x":430,"y":540,"wires":[["72c15da3.67bcb4","d0ebe41a.dbc338","d0c5c476.41e9f8","9394e281.e1baa","3ebe8a6.c2cc776","fd55653d.254eb8"]]},{"id":"72c15da3.67bcb4","type":"debug","z":"88029ce.abbab6","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":660,"y":520,"wires":[]},{"id":"d0ebe41a.dbc338","type":"debug","z":"88029ce.abbab6","name":"Fixed Daily Charges","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload.data.staticPrices.E1TOU.totalDailyPrice","targetType":"msg","statusVal":"","statusType":"auto","x":700,"y":580,"wires":[]},{"id":"d0c5c476.41e9f8","type":"debug","z":"88029ce.abbab6","name":"Peak KWH Price","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload.data.staticPrices.E1TOU.networkPeakKWHPrice","targetType":"msg","statusVal":"","statusType":"auto","x":690,"y":620,"wires":[]},{"id":"3ebe8a6.c2cc776","type":"debug","z":"88029ce.abbab6","name":"Off Peak KWH Price","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload.data.staticPrices.E1TOU.networkOffpeakKWHPrice","targetType":"msg","statusVal":"","statusType":"auto","x":700,"y":700,"wires":[]},{"id":"9394e281.e1baa","type":"debug","z":"88029ce.abbab6","name":"Shoulder KWH Price","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload.data.staticPrices.E1TOU.networkShoulderKWHPrice","targetType":"msg","statusVal":"","statusType":"auto","x":700,"y":660,"wires":[]},{"id":"fd55653d.254eb8","type":"change","z":"88029ce.abbab6","name":"","rules":[{"t":"set","p":"ambertest","pt":"global","to":"payload.data.variablePricesAndRenewables","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":850,"y":460,"wires":[[]]}]

```

Thanks

Craig

---

<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:** [12 June 2021 10:30 UTC](https://discourse.nodered.org/t/loop-through-an-array-and-return-index-of-a-specific-value/47063/2 "2021-06-12T10:30:05Z")

</div>

I think a function node using [findIndex](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/findIndex) and [slice](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/slice) are you best bet.

2 lines of code.

---

<div class="post-metadata">

**Author:** ![craigcurtin](https://avatars.discourse-cdn.com/v4/letter/c/94ad74/32.png) [@craigcurtin](https://discourse.nodered.org/u/craigcurtin)\
**Post date:** [12 June 2021 10:32 UTC](https://discourse.nodered.org/t/loop-through-an-array-and-return-index-of-a-specific-value/47063/3 "2021-06-12T10:32:13Z")

</div>

Yeah i always find i come back to those two line functions later and have to remind myself how they work !! Plus now my son is getting into this and being a teenager i know he will not document any of this

But time to roll the sleeves up i guess !!

Thanks for taking the time to respond

Craig

---

<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:** [12 June 2021 10:33 UTC](https://discourse.nodered.org/t/loop-through-an-array-and-return-index-of-a-specific-value/47063/4 "2021-06-12T10:33:35Z")

</div>

> [@craigcurtin](#):
>
> Yeah i always find i come back to those two line functions later and have to remind myself how they work

Add comments 😜

---

<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:** [12 June 2021 10:34 UTC](https://discourse.nodered.org/t/loop-through-an-array-and-return-index-of-a-specific-value/47063/5 "2021-06-12T10:34:13Z")

</div>

You can use JSONata in a change node

```auto
payload.data.variablePricesAndRenewables[periodType = "FORECAST"][[0..19]]

```

this will return first 20 items where field = FORECAST

---

<div class="post-metadata">

**Author:** ![craigcurtin](https://avatars.discourse-cdn.com/v4/letter/c/94ad74/32.png) [@craigcurtin](https://discourse.nodered.org/u/craigcurtin)\
**Post date:** [12 June 2021 10:34 UTC](https://discourse.nodered.org/t/loop-through-an-array-and-return-index-of-a-specific-value/47063/6 "2021-06-12T10:34:50Z")

</div>

HA HA !!!

There is always one in every room!!

Craig

---

<div class="post-metadata">

**Author:** ![craigcurtin](https://avatars.discourse-cdn.com/v4/letter/c/94ad74/32.png) [@craigcurtin](https://discourse.nodered.org/u/craigcurtin)\
**Post date:** [12 June 2021 10:36 UTC](https://discourse.nodered.org/t/loop-through-an-array-and-return-index-of-a-specific-value/47063/7 "2021-06-12T10:36:23Z")

</div>

WOW !!

This JSONATA stuff is so powerful but it scares me to death !

Can you point me to a good beginner resource that i can work through to gt a handle on it ?

Craig

---

<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:** [12 June 2021 10:57 UTC](https://discourse.nodered.org/t/loop-through-an-array-and-return-index-of-a-specific-value/47063/8 "2021-06-12T10:57:11Z")

</div>

Good is a subjective term  
here's where I go

> **[JSONata Documentation · JSONata](https://docs.jsonata.org/overview.html)**
>
> \## Introduction

and here is their exerciser, where you can add objects and expressions and test  
[https://try.jsonata.org/](https://try.jsonata.org/)

---

<div class="post-metadata">

**Author:** ![craigcurtin](https://avatars.discourse-cdn.com/v4/letter/c/94ad74/32.png) [@craigcurtin](https://discourse.nodered.org/u/craigcurtin)\
**Post date:** [12 June 2021 10:58 UTC](https://discourse.nodered.org/t/loop-through-an-array-and-return-index-of-a-specific-value/47063/9 "2021-06-12T10:58:10Z")

</div>

Good one thanks Man - can not believe how easy that one was (when you know how !)

Craig

---

<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:** [12 June 2021 10:59 UTC](https://discourse.nodered.org/t/loop-through-an-array-and-return-index-of-a-specific-value/47063/10 "2021-06-12T10:59:05Z")

</div>

I suggest getting fully competent with js first. Anything can be done in js, only certain things can be done with jsonata.

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [12 June 2021 14:36 UTC](https://discourse.nodered.org/t/loop-through-an-array-and-return-index-of-a-specific-value/47063/11 "2021-06-12T14:36:35Z")

</div>

> [@craigcurtin](#):
>
> Good one thanks Man - can not believe how easy that one was (when you know how !)

JSONata is one of those things that is great until it isn't. Some things are really simple to comprehend, some are simple to do and the rest takes longer than learning JavaScript from scratch 😱

My rule of thumb is if I can work it out in a few minutes then I'll use it. Otherwise I'll use a function node.

---

<div class="post-metadata">

**Author:** ![craigcurtin](https://avatars.discourse-cdn.com/v4/letter/c/94ad74/32.png) [@craigcurtin](https://discourse.nodered.org/u/craigcurtin)\
**Post date:** [13 June 2021 04:16 UTC](https://discourse.nodered.org/t/loop-through-an-array-and-return-index-of-a-specific-value/47063/12 "2021-06-13T04:16:51Z")

</div>

Yep i hear both you and Colin - and was 1/2 way through (blundering) writing a function to do it, when the one line answer came in - to say i was impressed is a very big understatement !

Craig

---

<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:** [13 June 2021 08:47 UTC](https://discourse.nodered.org/t/loop-through-an-array-and-return-index-of-a-specific-value/47063/13 "2021-06-13T08:47:33Z")

</div>

> [@craigcurtin](#):
>
> was 1/2 way through (blundering) writing a function to do it, when the one line answer came in

I agree with Colin and Julian here Craig.

JSONata can be great however in my experience, the following logic comes into play...

- If it takes more than a couple of minutes to write the JSONata, I revert to JS
- If there are many rows (500+) or sorting involved, I revert to JS (in certain operations, JSONata can easily be 300 times slower than the JS equivalent - [example](https://flows.nodered.org/flow/63f29373e826a9fd4943d679d43e725f))

Just be aware that JSONata is often a good choice but not always the right choice.

---

<div class="post-metadata">

**Author:** ![craigcurtin](https://avatars.discourse-cdn.com/v4/letter/c/94ad74/32.png) [@craigcurtin](https://discourse.nodered.org/u/craigcurtin)\
**Post date:** [16 June 2021 23:03 UTC](https://discourse.nodered.org/t/loop-through-an-array-and-return-index-of-a-specific-value/47063/14 "2021-06-16T23:03:22Z")

</div>

OK guys - i think you have me convinced on this one.

So i have a raw object (which is the return of a query) which has 144 elements in it - what i have done so far is the below

1. Using JSONATA - broken it into two seperate objects - one of actual price readings i.e. in the past and right now and the second for forecasts (i.e. in the future)

What i would like to do is extract the last reading in the Actual price readings - which should have the price right now and then extract the first 10 objects of the Forecasts which should have the estimated prices for the next 5 hours

Here is where i am at now with this

```auto
[{"id":"6028717e.571d7","type":"inject","z":"f2b4db9d.841788","name":"Check Prices","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":"300","topic":"","payload":"","payloadType":"date","x":150,"y":120,"wires":[["416043a5.94179c"]]},{"id":"416043a5.94179c","type":"function","z":"f2b4db9d.841788","name":"set headers and change Postcode here","func":"msg.headers = {};\nmsg.headers[\"Content-Type\"] = \"application/json\";\nmsg.payload = '{ \"postcode\":\"2069\" }';\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":260,"y":180,"wires":[["a2b6857.bc69078"]]},{"id":"a2b6857.bc69078","type":"http request","z":"f2b4db9d.841788","name":"Make a Request for Castle Cove (Ausgrid) Prices","method":"POST","ret":"obj","paytoqs":"ignore","url":"https://api.amberelectric.com.au/prices/listprices","tls":"","persist":false,"proxy":"","authType":"","x":230,"y":240,"wires":[["72c15da3.67bcb4","d0ebe41a.dbc338","d0c5c476.41e9f8","9394e281.e1baa","3ebe8a6.c2cc776","aaf92a51.b006e8","b801daea.9ec648","a947460.7f6c2b8","e4e36325.1f8b4","a62cfe87.7ed48","fd6dbd85.9381f","7bd5378c.12d2e8","1415f681.65f289"]]},{"id":"72c15da3.67bcb4","type":"debug","z":"f2b4db9d.841788","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":560,"y":180,"wires":[]},{"id":"d0ebe41a.dbc338","type":"debug","z":"f2b4db9d.841788","name":"Fixed Daily Charges","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload.data.staticPrices.E1TOU.totalDailyPrice","targetType":"msg","statusVal":"","statusType":"auto","x":600,"y":240,"wires":[]},{"id":"d0c5c476.41e9f8","type":"debug","z":"f2b4db9d.841788","name":"Peak KWH Price","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload.data.staticPrices.E1TOU.networkPeakKWHPrice","targetType":"msg","statusVal":"","statusType":"auto","x":590,"y":400,"wires":[]},{"id":"3ebe8a6.c2cc776","type":"debug","z":"f2b4db9d.841788","name":"Off Peak KWH Price","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload.data.staticPrices.E1TOU.networkOffpeakKWHPrice","targetType":"msg","statusVal":"","statusType":"auto","x":600,"y":560,"wires":[]},{"id":"9394e281.e1baa","type":"debug","z":"f2b4db9d.841788","name":"Shoulder KWH Price","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload.data.staticPrices.E1TOU.networkShoulderKWHPrice","targetType":"msg","statusVal":"","statusType":"auto","x":600,"y":480,"wires":[]},{"id":"aaf92a51.b006e8","type":"change","z":"f2b4db9d.841788","name":"","rules":[{"t":"set","p":"amber.Peak_KWH_Price","pt":"global","to":"payload.data.staticPrices.E1TOU.networkPeakKWHPrice","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":640,"y":440,"wires":[[]]},{"id":"b801daea.9ec648","type":"change","z":"f2b4db9d.841788","name":"","rules":[{"t":"set","p":"amber.Shoulder_KWH_Price","pt":"global","to":"payload.data.staticPrices.E1TOU.networkShoulderKWHPrice","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":650,"y":520,"wires":[[]]},{"id":"a947460.7f6c2b8","type":"change","z":"f2b4db9d.841788","name":"","rules":[{"t":"set","p":"amber.OffPeak_KWH_Price","pt":"global","to":"payload.data.staticPrices.E1TOU.networkOffpeakKWHPrice","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":650,"y":600,"wires":[[]]},{"id":"e4e36325.1f8b4","type":"change","z":"f2b4db9d.841788","name":"","rules":[{"t":"set","p":"amber.Total_Fixed_Daily_Charges","pt":"global","to":"payload.data.staticPrices.E1TOU.totalDailyPrice","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":670,"y":300,"wires":[[]]},{"id":"ba2d4b33.6e1368","type":"comment","z":"f2b4db9d.841788","name":"Set ToU Pricing","info":"","x":180,"y":300,"wires":[]},{"id":"a62cfe87.7ed48","type":"function","z":"f2b4db9d.841788","name":"Set Prices","func":"// Set all the fixed KWH prices (inc GST)\n\n\n// Carbon Neutral Cost\na = 0.11;\n\n// Environmental Certificate Cost\nb = 2.0361;\n\n// Market Charges\nc = 0.1595;\n\n// Price Protection Hedging\nd = 0.55;\n\nvar Static_KWH_Pricing = 1.04*(a + b + c + d);\nvar CurrentTOUPrice = 1 ;\n\nglobal.set('amber.Static_KWH_Pricing',Static_KWH_Pricing);\n\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":170,"y":440,"wires":[[]]},{"id":"7bd5378c.12d2e8","type":"change","z":"f2b4db9d.841788","name":"","rules":[{"t":"set","p":"ambertest","pt":"global","to":"payload.data.variablePricesAndRenewables","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":960,"y":200,"wires":[[]]},{"id":"fd6dbd85.9381f","type":"change","z":"f2b4db9d.841788","name":"","rules":[{"t":"set","p":"AmberEstimatePriceObject","pt":"global","to":"payload.data.variablePricesAndRenewables[periodType = \"FORECAST\"][[0..19]]","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":1010,"y":160,"wires":[[]]},{"id":"1415f681.65f289","type":"change","z":"f2b4db9d.841788","name":"","rules":[{"t":"set","p":"AmberActualPriceObject","pt":"global","to":"payload.data.variablePricesAndRenewables[periodType = \"ACTUAL\"][[0..50]]","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":1000,"y":240,"wires":[["1b2208a5.6dd677"]]},{"id":"1b2208a5.6dd677","type":"debug","z":"f2b4db9d.841788","name":"Output 1","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1180,"y":340,"wires":[]}]

```

Am i going to be able to make this much cleaner and easier to read by moving it to a function or should i persist with the jsonata and a couple more change nodes ?

Craig

---

<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 June 2021 23:03 UTC](https://discourse.nodered.org/t/loop-through-an-array-and-return-index-of-a-specific-value/47063/15 "2021-06-30T23:03:52Z")

</div>

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