Read content of highest object value

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

Thanks :slight_smile:

Kind regards,
Roy

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

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?

Regarding jsonata expression, I'm not familiar with that :innocent:

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

[{"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

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

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

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

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