# JSON Array Filtering - Function Node

**URL:** <https://discourse.nodered.org/t/json-array-filtering-function-node/42827>\
**Category:** General\
**Created:** [18 March 2021 15:26 UTC](https://discourse.nodered.org/t/json-array-filtering-function-node/42827 "2021-03-18T15:26:41Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![franco101](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/franco101/32/32675_2.png) [@franco101](https://discourse.nodered.org/u/franco101)\
**Post date:** [18 March 2021 15:26 UTC](https://discourse.nodered.org/t/json-array-filtering-function-node/42827/1 "2021-03-18T15:26:42Z")

</div>

Hello,

Using the function node please could someone help me filter the area\_id value when its set to RT in the below array?

```auto
msg.payload = msg.payload.CA_MSG.filter(el => el.area_id.includes("RT"))
return msg;

```

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/e/0e71c156b60695e914cc49c943db29b3304d891b.png)

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [18 March 2021 15:47 UTC](https://discourse.nodered.org/t/json-array-filtering-function-node/42827/2 "2021-03-18T15:47:49Z")

</div>

See this post from earlier today which asks an almost identical question. [Filter Array of Objects](https://discourse.nodered.org/t/filter-array-of-objects/42808)

**[Edit]** Note that if you are using the filter function you have to call filter on the Array, which is msg.payload.trains, not msg.payload.CA\_MSG, and you have to check for equality (at least I assume that is what you want) so the test is `el.area_id === "RT")`

**[Edit]** as pointed out below that should be `el.CA_MSG.area_id`

---

<div class="post-metadata">

**Author:** ![franco101](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/franco101/32/32675_2.png) [@franco101](https://discourse.nodered.org/u/franco101)\
**Post date:** [18 March 2021 16:28 UTC](https://discourse.nodered.org/t/json-array-filtering-function-node/42827/3 "2021-03-18T16:28:30Z")

</div>

Thanks for your reply Colin.

I am trying to filter every object that has RT set in the area\_id field.

Could you help as it's still not filtering properly?

```auto
msg.payload = msg.payload(el => el.area_id === "RT")

```

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/9/a92949a5c91ca54d2cd3a99353a38049a74e73df.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 March 2021 16:45 UTC](https://discourse.nodered.org/t/json-array-filtering-function-node/42827/4 "2021-03-18T16:45:03Z")

</div>

Looking at your original debug image.  
Your object paths are incorrect, the array is in msg.payload.trains, and area\_id is in CA\_MSG.

```auto
msg.payload = msg.payload.trains.filter(el => el.CA_MSG.area_id === "RT")

```

also you missed the filter function name.

---

<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:** [17 May 2021 16:45 UTC](https://discourse.nodered.org/t/json-array-filtering-function-node/42827/5 "2021-05-17T16:45:38Z")

</div>

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