Remove an array depending on a condition

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.

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

Thanks for your help!

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

msg.payload = msg.payload.departures.all.filter( el => el.destination_name !== "Weybridge" )
1 Like

It works!!! Thank you so much @UnborN

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