# Read content of highest object value

**URL:** <https://discourse.nodered.org/t/read-content-of-highest-object-value/41953>\
**Category:** General\
**Created:** [3 March 2021 13:50 UTC](https://discourse.nodered.org/t/read-content-of-highest-object-value/41953 "2021-03-03T13:50:00Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Spyker](https://avatars.discourse-cdn.com/v4/letter/s/ea5d25/32.png) [@Spyker](https://discourse.nodered.org/u/Spyker)\
**Post date:** [3 March 2021 13:50 UTC](https://discourse.nodered.org/t/read-content-of-highest-object-value/41953/1 "2021-03-03T13:50:00Z")

</div>

Hi all,

I'm creating a flow to sent my daily profits to a spreadsheet, but the thing is, every day a new entry is added to payload.extra.dailyTCVStats. So I need to read the TCV value within the highest object value ( in this case 5) because that is the current day.  
Does someone has a solution for this?  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/6/4674647deb1f04eba01b053b416c8e4480c8d864.png)

Thanks 🙂

Kind regards,  
Roy

---

<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 March 2021 14:09 UTC](https://discourse.nodered.org/t/read-content-of-highest-object-value/41953/2 "2021-03-03T14:09:58Z")

</div>

Hi Spyker,

Can you copy paste the actual data to replicate this into a flow example ?  
(Use the copy value icon on the root of the msg)

You can loop throught the array with JS to find the highest value  
or use a Jsonata expression in a Change node to sort by descending TCV and pick the first in order.

Example:  
[https://try.jsonata.org/Q5e7wFAz4](https://try.jsonata.org/Q5e7wFAz4)

---

<div class="post-metadata">

**Author:** ![Spyker](https://avatars.discourse-cdn.com/v4/letter/s/ea5d25/32.png) [@Spyker](https://discourse.nodered.org/u/Spyker)\
**Post date:** [3 March 2021 15:01 UTC](https://discourse.nodered.org/t/read-content-of-highest-object-value/41953/3 "2021-03-03T15:01:09Z")

</div>

Hi @UnborN,

I cannot page the actual data as it's 4 times the amount of data I'm allowed to send in this message..  
Is this helpful?

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/9/99ad7b72cff96136a16fefe01279a8da8656264f.png)

Regarding jsonata expression, I'm not familiar with that 😇

---

<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 March 2021 15:20 UTC](https://discourse.nodered.org/t/read-content-of-highest-object-value/41953/4 "2021-03-03T15:20:16Z")

</div>

Here is an example of how you can use it with Jsonata. I tried to replicate part of your data.

```auto
[{"id":"e43ee91.221d298","type":"inject","z":"42c8e9e3.fd4a18","name":"fake data","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"extra\":{\"dailyTCVStats\":[{\"date\":\"2-3-2021\",\"minTCV\":1.02999,\"TCV\":0.02999,\"order\":5},{\"date\":\"2-4-2021\",\"minTCV\":0.02999,\"TCV\":1.02999,\"order\":5},{\"date\":\"2-5-2021\",\"minTCV\":0.02999,\"TCV\":3.22999,\"order\":5},{\"date\":\"2-6-2021\",\"minTCV\":0.02999,\"TCV\":7.119,\"order\":5}]}}","payloadType":"json","x":360,"y":1380,"wires":[["73432244.a4862c","5dc46cdb.54a544"]]},{"id":"73432244.a4862c","type":"change","z":"42c8e9e3.fd4a18","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload.extra.dailyTCVStats^(>TCV)","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":540,"y":1340,"wires":[["a74b557f.e9e348"]]},{"id":"a74b557f.e9e348","type":"debug","z":"42c8e9e3.fd4a18","name":"1","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":710,"y":1340,"wires":[]},{"id":"5dc46cdb.54a544","type":"change","z":"42c8e9e3.fd4a18","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload.extra.dailyTCVStats^(>TCV)[0]","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":540,"y":1420,"wires":[["6fb59b13.d186fc"]]},{"id":"6fb59b13.d186fc","type":"debug","z":"42c8e9e3.fd4a18","name":"2","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":710,"y":1420,"wires":[]},{"id":"b5c1b4d8.ed7b48","type":"comment","z":"42c8e9e3.fd4a18","name":"sort by highest TCV","info":"","x":550,"y":1280,"wires":[]},{"id":"51bff91c.20dc7","type":"comment","z":"42c8e9e3.fd4a18","name":"sort by highest TCV and take only the highest","info":"","x":630,"y":1480,"wires":[]}]

```

You can read more about jsonata expressions and its _order-by_ operator [here](https://docs.jsonata.org/path-operators#---order-by)

---

<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:** [3 March 2021 16:54 UTC](https://discourse.nodered.org/t/read-content-of-highest-object-value/41953/5 "2021-03-03T16:54:12Z")

</div>

> [@Spyker](#):
>
> So I need to read the TCV value within the highest object value ( in this case 5) because that is the current day.

If you mean last item in the array then with a function node...

```auto
var lastIndex = msg.payload.extra.dailyTCVStats.length-1;
msg.payload = msg.payload.extra.dailyTCVStats[lastIndex].TCV;
return msg;

```

---

<div class="post-metadata">

**Author:** ![Spyker](https://avatars.discourse-cdn.com/v4/letter/s/ea5d25/32.png) [@Spyker](https://discourse.nodered.org/u/Spyker)\
**Post date:** [5 March 2021 09:18 UTC](https://discourse.nodered.org/t/read-content-of-highest-object-value/41953/6 "2021-03-05T09:18:14Z")

</div>

Thanks @UnborN and @Steve-Mcl. I have it solved with the config Steve posted 🥳

---

<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:** [19 March 2021 09:18 UTC](https://discourse.nodered.org/t/read-content-of-highest-object-value/41953/7 "2021-03-19T09:18:40Z")

</div>

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