# Finding a Property in a constantly updating Array

**URL:** <https://discourse.nodered.org/t/finding-a-property-in-a-constantly-updating-array/77736>\
**Category:** General\
**Tags:** function-node\
**Created:** [20 April 2023 10:07 UTC](https://discourse.nodered.org/t/finding-a-property-in-a-constantly-updating-array/77736 "2023-04-20T10:07:11Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![CJ10](https://avatars.discourse-cdn.com/v4/letter/c/f6c823/32.png) [@CJ10](https://discourse.nodered.org/u/CJ10)\
**Post date:** [20 April 2023 10:07 UTC](https://discourse.nodered.org/t/finding-a-property-in-a-constantly-updating-array/77736/1 "2023-04-20T10:07:11Z")

</div>

I would like to access an array of 3 values. They are in a property, but during each pass through my flow, a new property is added, containing a new set of 3 values. Basically, I always need to access the final property in the list.

![Screenshot 2023-04-20 at 11.40.25](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/9/2900f5c2c9d89d70fb7dee5796422f5137e4dbe7.png)

In the screenshot, in this case, I want to locate the ....gx:cord.[89] but on the next loop through the flow it be ....gx:cord[90]. I know how to search and locate a property but not when I dont know the object property anem because it is increasing.

I should add that I dont always start my flow when the property==1. It could already be up to 70 before I read the payload. So I always need to access the last one in the list.

How do I find the last or highest value in the property?

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [20 April 2023 10:38 UTC](https://discourse.nodered.org/t/finding-a-property-in-a-constantly-updating-array/77736/2 "2023-04-20T10:38:32Z")

</div>

Here is a simple flow grabbing the last item in an array. Hopefully it will help you out.

```auto
[{"id":"c69aa3d5ec72345b","type":"inject","z":"35590331d81b2967","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[\"Banana\", \"Orange\", \"Apple\", \"Mango\"]","payloadType":"json","x":190,"y":200,"wires":[["b150da40085ebbf3"]]},{"id":"b150da40085ebbf3","type":"function","z":"35590331d81b2967","name":"function 4","func":"let arr = msg.payload //set arr as the array\nlet arr_len = arr.length //get the length of the array\n// set the payload to the last element in the array\nmsg.payload = arr[arr_len - 1]\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":380,"y":200,"wires":[["ac9640f2921e025c"]]},{"id":"ac9640f2921e025c","type":"debug","z":"35590331d81b2967","name":"debug 334","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":570,"y":200,"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:** [20 April 2023 12:59 UTC](https://discourse.nodered.org/t/finding-a-property-in-a-constantly-updating-array/77736/3 "2023-04-20T12:59:35Z")

</div>

You can also in a change node  
set `msg.``payload`  
to JSONata `J:``$$.payload[[-3..-1]]`  
which should return the last 3 array items.

---

<div class="post-metadata">

**Author:** ![CJ10](https://avatars.discourse-cdn.com/v4/letter/c/f6c823/32.png) [@CJ10](https://discourse.nodered.org/u/CJ10)\
**Post date:** [20 April 2023 13:56 UTC](https://discourse.nodered.org/t/finding-a-property-in-a-constantly-updating-array/77736/4 "2023-04-20T13:56:52Z")

</div>

That very much did help me out.  
Took me a few attempts at entering my payload path correctly though!

---

<div class="post-metadata">

**Author:** ![CJ10](https://avatars.discourse-cdn.com/v4/letter/c/f6c823/32.png) [@CJ10](https://discourse.nodered.org/u/CJ10)\
**Post date:** [20 April 2023 14:00 UTC](https://discourse.nodered.org/t/finding-a-property-in-a-constantly-updating-array/77736/5 "2023-04-20T14:00:51Z")

</div>

Thanks but where does my path go int his expression?

Do i leave the rule box to literally, `msg.` `payload`

Then enter the expression as;  
_my path_ `[[-3..-1]]`

Sorry but I've never used JSONata before

---

<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:** [20 April 2023 14:07 UTC](https://discourse.nodered.org/t/finding-a-property-in-a-constantly-updating-array/77736/6 "2023-04-20T14:07:23Z")

</div>

Your path goes where payload is, so if path is `msg.payload["gx:coord"]`.  
it would be

```auto
 $$.payload.`gx:coord`[[-3..-1]]

```

The "gx:cord" has non standard property characters, so needs to be in quotes.  
e.g.

```auto
[{"id":"61e991d9a076e59b","type":"inject","z":"b9860b4b9de8c8da","name":"","props":[{"p":"payload[\"gx:coord\"]","v":"[\"Banana\", \"Orange\", \"Apple\", \"Mango\"]","vt":"json"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":250,"y":500,"wires":[["7e8749bdb091d933"]]},{"id":"7e8749bdb091d933","type":"change","z":"b9860b4b9de8c8da","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":" $$.payload.`gx:coord`[[-3..-1]]","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":420,"y":500,"wires":[["a9ce98808a80655f"]]},{"id":"a9ce98808a80655f","type":"debug","z":"b9860b4b9de8c8da","name":"debug 334","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":630,"y":500,"wires":[]}]

```

---

<div class="post-metadata">

**Author:** ![CJ10](https://avatars.discourse-cdn.com/v4/letter/c/f6c823/32.png) [@CJ10](https://discourse.nodered.org/u/CJ10)\
**Post date:** [20 April 2023 14:33 UTC](https://discourse.nodered.org/t/finding-a-property-in-a-constantly-updating-array/77736/7 "2023-04-20T14:33:10Z")

</div>

> [@E1cid](#):
>
> `[{"id":"61e991d9a076e59b","type":"inject","z":"b9860b4b9de8c8da","name":"","props":[{"p":"payload[\"gx:coord\"]","v":"[\"Banana\", \"Orange\", \"Apple\", \"Mango\"]","vt":"json"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":250,"y":500,"wires":[["7e8749bdb091d933"]]},{"id":"7e8749bdb091d933","type":"change","z":"b9860b4b9de8c8da","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":" $.payload.`gx:coord`[[-3..-1]]","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":420,"y":500,"wires":[["a9ce98808a80655f"]]},{"id":"a9ce98808a80655f","type":"debug","z":"b9860b4b9de8c8da","name":"debug 334","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":630,"y":500,"wires":[]}]`

My Actual path is;  
payload.kml.Folder[0].Folder[0].Placemark[0]["gx:Track"][0]["gx:coord"]

So the path needs to be something like this? I've tried a few variations but I can't seem to get it to work. Can you see what I am doing wrong?  
$$.payload.kml.Folder[0].Folder[0].Placemark[0]`gx:Track`[0]`gx:coord`[[-3..-1]]

Thanks

---

<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:** [20 April 2023 14:50 UTC](https://discourse.nodered.org/t/finding-a-property-in-a-constantly-updating-array/77736/8 "2023-04-20T14:50:03Z")

</div>

> [@E1cid](#):
>
> The "gx:cord" has non standard property characters, so needs to be in quotes.

You are not following my reply advise about non-standard property name characters and quoting them. You also have missing periods.

```auto
$$.payload.kml.Folder[0].Folder[0].Placemark[0]."gx:Track"[0]."gx:coord"[[-3..-1]]

```

---

<div class="post-metadata">

**Author:** ![CJ10](https://avatars.discourse-cdn.com/v4/letter/c/f6c823/32.png) [@CJ10](https://discourse.nodered.org/u/CJ10)\
**Post date:** [20 April 2023 15:29 UTC](https://discourse.nodered.org/t/finding-a-property-in-a-constantly-updating-array/77736/9 "2023-04-20T15:29:47Z")

</div>

Ah I see, I was using apostrophes instead of quotes, as well as not having the periods.

Thanks that works.

What are the non standard propert characters?

---

<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:** [20 April 2023 15:32 UTC](https://discourse.nodered.org/t/finding-a-property-in-a-constantly-updating-array/77736/10 "2023-04-20T15:32:34Z")

</div>

you can use double quotes, single quotes or backticks.

There are many but most are language operators, as in your example `:` but there are many others  
Best to use A-z a-z 0-9 and underscore to avoid the situation.

You can tell there are non-standard chars as the copy path put the object name in square bracket notation, as non-standard chars had been used.

---

<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:** [4 May 2023 15:32 UTC](https://discourse.nodered.org/t/finding-a-property-in-a-constantly-updating-array/77736/11 "2023-05-04T15:32:57Z")

</div>

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