# Remove an array depending on a condition

**URL:** <https://discourse.nodered.org/t/remove-an-array-depending-on-a-condition/67771>\
**Category:** General\
**Created:** [15 September 2022 22:20 UTC](https://discourse.nodered.org/t/remove-an-array-depending-on-a-condition/67771 "2022-09-15T22:20:07Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![notownblues](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/notownblues/32/67810_2.png) [@notownblues](https://discourse.nodered.org/u/notownblues)\
**Post date:** [15 September 2022 22:20 UTC](https://discourse.nodered.org/t/remove-an-array-depending-on-a-condition/67771/1 "2022-09-15T22:20:07Z")

</div>

Hi,

I'm using the TransportAPI to pull the next train departures from a station to a destination. There's multiple destinations (trains) I can take but one of them, the Weybridge one is 1h20 so I would like to exclude it from my next trains. The object comes in a form of arrays for the next trains, and I would like to delete an entire array that contains the destination\_name "Weybridge" as shown in the screenshot.

I've been looking for hours and managed to get somewhere with this but it deleted the message rather than deleting the entire array.

```auto
if (msg.payload.departures.all[0].destination_name == "Weybridge") {
    delete msg.payload.departures.all[0];
}

```

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/d/7d037f6dc2a5822b130a7a0a4d543274d076324b.png)

Thanks for your help!

---

<div class="post-metadata">

**Author:** ![UnborN](https://avatars.discourse-cdn.com/v4/letter/u/4491bb/32.png) [@UnborN](https://discourse.nodered.org/u/UnborN)\
**Post date:** [15 September 2022 22:34 UTC](https://discourse.nodered.org/t/remove-an-array-depending-on-a-condition/67771/2 "2022-09-15T22:34:40Z")

</div>

Hello . you can try using Javascript's `filter()` method on the array to filter all departures that are not "Weybridge"

```auto
msg.payload = msg.payload.departures.all.filter( el => el.destination_name !== "Weybridge" )

```

---

<div class="post-metadata">

**Author:** ![notownblues](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/notownblues/32/67810_2.png) [@notownblues](https://discourse.nodered.org/u/notownblues)\
**Post date:** [15 September 2022 22:53 UTC](https://discourse.nodered.org/t/remove-an-array-depending-on-a-condition/67771/3 "2022-09-15T22:53:07Z")

</div>

It works!!! Thank you so much @UnborN

---

<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:** [29 September 2022 22:54 UTC](https://discourse.nodered.org/t/remove-an-array-depending-on-a-condition/67771/4 "2022-09-29T22:54:01Z")

</div>

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