skip to Main Content

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


  1. You can use filter method, and on each item in the array, check if every value in event_dates object are with null and then filter this item.

    const arr = [
    {
      "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
        }
      }
    ]
    
    const result = arr.filter(item => Object.values(item.event_dates).every(item => item === null))
    
    console.log(result)
    Login or Signup to reply.
  2. As long as all the objects are structured the same way you could simply do

    const filteredArray = inputArray.filter(object => {
      object.event_dates.starting_day !== "null"
    }
    

    I will point out that the first object has an extra closing curly bracket

    {1
      "id": "abc123",
      "name": 
        {2
          "fi": "The name of the event in finnish",
          "en": "The name of the event in english",
          "sv": "samma på svenska",
          "zh": "标题"
        1},
          "description": {3
            "intro": "",
            "body": ""
        2},
          "event_dates": {4
            "starting_day": "2025-10-24T16:00:00.000Z",
            "ending_day": "2025-10-24T17:00:00.000Z"
        3}
      4} 
    5},
    

    I would remove the 4th closing curly.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search