# How to always use the last item in an array?

**URL:** <https://discourse.nodered.org/t/how-to-always-use-the-last-item-in-an-array/73553>\
**Category:** General\
**Tags:** switch-node\
**Created:** [11 January 2023 19:02 UTC](https://discourse.nodered.org/t/how-to-always-use-the-last-item-in-an-array/73553 "2023-01-11T19:02:18Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![network\_potato](https://avatars.discourse-cdn.com/v4/letter/n/59ef9b/32.png) [@network\_potato](https://discourse.nodered.org/u/network_potato)\
**Post date:** [11 January 2023 19:02 UTC](https://discourse.nodered.org/t/how-to-always-use-the-last-item-in-an-array/73553/1 "2023-01-11T19:02:18Z")

</div>

I have a switch node where I check a value. Feeding into this switch node is an array.

Sometimes the array consists of 1 item, sometimes 2, sometimes 3, etc.

How can I make the switch node always check the last item in the array for the value?

Instead of specifying the number. For example: `payload.example[1].customer.firstName` or `payload.example[2].customer.firstName`.

It would always check #2 if the array had 2, always check #4 if the array had 4.

Did that make sense?

---

<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:** [11 January 2023 20:17 UTC](https://discourse.nodered.org/t/how-to-always-use-the-last-item-in-an-array/73553/2 "2023-01-11T20:17:14Z")

</div>

You can use JSONata in the property field.  
e.g.

```auto
[{"id":"c828b9168a3f213f","type":"inject","z":"da8a6ef0b3c9a5c8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"example\":[{\"customer\":{\"firstName\":\"tom\"}},{\"customer\":{\"firstName\":\"dick\"}},{\"customer\":{\"firstName\":\"harry\"}}]}","payloadType":"json","x":250,"y":3100,"wires":[["51466519ddf4a14b"]]},{"id":"51466519ddf4a14b","type":"switch","z":"da8a6ef0b3c9a5c8","name":"","property":"$$.payload.example[-1].customer.firstName","propertyType":"jsonata","rules":[{"t":"eq","v":"harry","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":430,"y":3080,"wires":[["a81d87f562b74deb"]]},{"id":"a81d87f562b74deb","type":"debug","z":"da8a6ef0b3c9a5c8","name":"debug 225","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":650,"y":3120,"wires":[]}]

```

```auto
$$.payload.example[-1].customer.firstName

```

---

<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:** [11 January 2023 23:28 UTC](https://discourse.nodered.org/t/how-to-always-use-the-last-item-in-an-array/73553/3 "2023-01-11T23:28:29Z")

</div>

The traditional way is to do:

```auto
payload.example[payload.example.length - 1].customer.firstName

```

But a more modern way is to:

```auto
payload.example.slice(-1)[0].customer.firstName

```

Or:

```auto
payload.example.pop().customer.firstName

```

Though that actually does change the original which is probably not what you want.

Or even:

```auto
const [last,] = payload.example.reverse()
console.log(last.customer.firstName)

```

But if you can cope with only the very latest version of JS (needs node.js v16.6+):

```auto
payload.example.at(-1).customer.firstName

```

---

<div class="post-metadata">

**Author:** ![network\_potato](https://avatars.discourse-cdn.com/v4/letter/n/59ef9b/32.png) [@network\_potato](https://discourse.nodered.org/u/network_potato)\
**Post date:** [18 January 2023 12:10 UTC](https://discourse.nodered.org/t/how-to-always-use-the-last-item-in-an-array/73553/4 "2023-01-18T12:10:07Z")

</div>

I copied your example flow, but it outputs the whole array through the switch node?

---

<div class="post-metadata">

**Author:** ![network\_potato](https://avatars.discourse-cdn.com/v4/letter/n/59ef9b/32.png) [@network\_potato](https://discourse.nodered.org/u/network_potato)\
**Post date:** [18 January 2023 12:11 UTC](https://discourse.nodered.org/t/how-to-always-use-the-last-item-in-an-array/73553/5 "2023-01-18T12:11:03Z")

</div>

> [@TotallyInformation](#):
>
> The traditional way is to do:
> 
> ```auto
> payload.example[payload.example.length - 1].customer.firstName
> 
> ```
> 
> But a more modern way is to:
> 
> ```auto
> payload.example.slice(-1)[0].customer.firstName
> 
> ```

First one returns nothing, the second one gives the following error:

```auto
Invalid JSONata expression: Attempted to invoke a non-function

```

---

<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:** [18 January 2023 13:16 UTC](https://discourse.nodered.org/t/how-to-always-use-the-last-item-in-an-array/73553/6 "2023-01-18T13:16:47Z")

</div>

> [@network\_potato](#):
>
> First one returns nothing

Then the data you listed isn't what you are trying to handle. In any case, that is the correct syntax, you simply need to adapt it to your data.

> [@network\_potato](#):
>
> the second one gives the following error

Again, almost certainly your data isn't what you assume (or you are using an old, outdated version of node.js):  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/9/d94d5b66365b5e680b44fd7b2f06c3ede385dcae.png)

---

<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:** [18 January 2023 13:34 UTC](https://discourse.nodered.org/t/how-to-always-use-the-last-item-in-an-array/73553/7 "2023-01-18T13:34:43Z")

</div>

> [@network\_potato](#):
>
> How can I make the switch node always check the last item in the array for the value?

I answered this question, you never asked to output the last value.

Please be specific, what do you actually require

---

<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:** [18 January 2023 13:40 UTC](https://discourse.nodered.org/t/how-to-always-use-the-last-item-in-an-array/73553/8 "2023-01-18T13:40:41Z")

</div>

Ah, I understand now, you were asking about a switch node not about JavaScript. Obviously I was giving you the JavaScript code.

---

<div class="post-metadata">

**Author:** ![network\_potato](https://avatars.discourse-cdn.com/v4/letter/n/59ef9b/32.png) [@network\_potato](https://discourse.nodered.org/u/network_potato)\
**Post date:** [19 January 2023 14:25 UTC](https://discourse.nodered.org/t/how-to-always-use-the-last-item-in-an-array/73553/9 "2023-01-19T14:25:44Z")

</div>

Sorry my bad, this was exactly the solution I was looking for. Thank you.

---

<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:** [2 February 2023 14:26 UTC](https://discourse.nodered.org/t/how-to-always-use-the-last-item-in-an-array/73553/10 "2023-02-02T14:26:17Z")

</div>

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