I have an array of objects that contains events looking like this for example:
[
{
"id": "abc123",
"name":
{
"fi": "The name of the event in finnish",
"en": "The name of the event in english",
"sv": "samma på svenska",
"zh": "标题"
},
"description": {
"intro": "",
"body": ""
},
"event_dates": {
"starting_day": "2025-10-24T16:00:00.000Z",
"ending_day": "2025-10-24T17:00:00.000Z"
}
}
},
{
"id": "123efg",
"name":
{
"fi": "The name of the event in finnish",
"en": "The name of the event in english",
"sv": "samma på svenska",
"zh": "标题"
},
"description": {
"intro": "",
"body": ""
},
"event_dates": {
"starting_day": null,
"ending_day": null
}
}
]
However, some of the starting_day and ending_day values in the array are null. I need to write a function that would filter all the events where the date values are null and print out the array that only contains the events where the dates are not null. What could be the cleanest way to accomplish that?
2
Answers
You can use
filter
method, and on each item in the array, check ifevery
value
inevent_dates
object are withnull
and then filter this item.As long as all the objects are structured the same way you could simply do
I will point out that the first object has an extra closing curly bracket
I would remove the 4th closing curly.