JSON Array Filtering - Function Node

Hello,

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

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

image

See this post from earlier today which asks an almost identical question. Filter Array of Objects

[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

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?

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

image

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.

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

also you missed the filter function name.

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