# Filter messages with a changing array size

**URL:** <https://discourse.nodered.org/t/filter-messages-with-a-changing-array-size/71447>\
**Category:** General\
**Created:** [29 November 2022 13:07 UTC](https://discourse.nodered.org/t/filter-messages-with-a-changing-array-size/71447 "2022-11-29T13:07:32Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![gspritchard](https://avatars.discourse-cdn.com/v4/letter/g/439d5e/32.png) [@gspritchard](https://discourse.nodered.org/u/gspritchard)\
**Post date:** [29 November 2022 13:07 UTC](https://discourse.nodered.org/t/filter-messages-with-a-changing-array-size/71447/1 "2022-11-29T13:07:32Z")

</div>

Hi,

I have an array output that varies in size depending on the data it is sending. The device sends data every 60 seconds on an occupancy count for a building. Every time the data changes, it added another item into the array, so if nobody enters, an empty message is sent, if 1 person enters over 60 seconds, the array contains one value, however if 10 people enter, there are 10 entries in the array. If 10 enter, and 5 leave, there are 15 values in the array, etc, etc.

I am looking for a way to grab the values out of the array, regardless of the index number. I am using a switch node to filter the empty values out, but if I do anything using = msg.payload[0].value, then it works for the first array item only, does that make sense? The "live" number may be vastly different to this.

If I cannot do it for each item, how would I always grab the LAST item in an array?

---

<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:** [29 November 2022 13:27 UTC](https://discourse.nodered.org/t/filter-messages-with-a-changing-array-size/71447/2 "2022-11-29T13:27:55Z")

</div>

Have a read of: [Array - JavaScript | MDN (mozilla.org)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)

```auto
> x = [1,2,3,4,5,6,7,8,9]
[
  1, 2, 3, 4, 5,
  6, 7, 8, 9
]
> x[0]
1
> x[x.length-1]
9

```

There are lots of array functions and several ways of "walking" an array for each element.

Of course, if all you want is the count then `x.length - 1` is what you want.

---

<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:** [28 January 2023 13:28 UTC](https://discourse.nodered.org/t/filter-messages-with-a-changing-array-size/71447/3 "2023-01-28T13:28:54Z")

</div>

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