Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

118
Views
filter array of objects with less than and present date

I have objects with different dates. How I can filter out the past dates and present date. I have dates with from past and future, I want only to past dates and the current dates from the objects. I try filtering but I'm not sure what wrong I'm doing My CODE :

const arr = [{
    "id": "e18dae03-99c7-4ffa-be2f-a595fc7fad15",
    "event_date": "2021-09-30T01:00:00.000Z",
    "alert_name": null,
    "alert_level": null,
    "alert_action": null,
    "alert_details": null,
},
{
    "id": "e18dae03-99c7-4ffa-be2f-a595fc7fad15",
    "event_date": "2021-10-30T01:00:00.000Z",
    "alert_name": null,
    "alert_level": null,
    "alert_action": null,
    "alert_details": null,
}
{
    "id": "765a508f-fb47-4de4-bf69-19f0db38ccfc",
    "event_date": "2021-09-17T22:03:00.000Z",
    "alert_name": null,
    "alert_level": null,
    "alert_action": null,
    "alert_details": null,
},
{
    "id": "bc07a114-43f4-49a1-bb6c-fec801aee749",
    "longitude": null,
    "event_date": "2021-11-19T07:51:00.000Z",
    "alert_name": null,
    "alert_level": null,
    "alert_action": null,
    "alert_details": null,
}
]


const filterByExpiration = () => arr.filter(({ event_date }) => event_date <= new Date());



about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

event_date is a string. You should convert it to a Date object first.

const filterByExpiration = () => arr.filter(({ event_date }) => new Date(event_date) <= new Date());

Or compare it to an ISO date string

const filterByExpiration = () => arr.filter(({ event_date }) => event_date <= new Date().toISOString());
about 4 years ago · Juan Pablo Isaza Report

0

You have to convert the node event_date in each node of the arr to date object to make comparison. As of now its a string.

Also you have to call filterByExpiration function. You have just defined that, it has not been called.

Working Fiddle

const arr = [{"id":"e18dae03-99c7-4ffa-be2f-a595fc7fad15","event_date":"2021-09-30T01:00:00.000Z","alert_name":null,"alert_level":null,"alert_action":null,"alert_details":null},{"id":"e18dae03-99c7-4ffa-be2f-a595fc7fad15","event_date":"2021-10-30T01:00:00.000Z","alert_name":null,"alert_level":null,"alert_action":null,"alert_details":null},{"id":"765a508f-fb47-4de4-bf69-19f0db38ccfc","event_date":"2021-09-17T22:03:00.000Z","alert_name":null,"alert_level":null,"alert_action":null,"alert_details":null},{"id":"bc07a114-43f4-49a1-bb6c-fec801aee749","longitude":null,"event_date":"2021-11-19T07:51:00.000Z","alert_name":null,"alert_level":null,"alert_action":null,"alert_details":null}];
const filterByExpiration = () => arr.filter(({ event_date }) => new Date(event_date) <= new Date());
console.log(filterByExpiration())

about 4 years ago · Juan Pablo Isaza Report

0

Strings can not be compared with Dates. You need to parse event_date to a Date using new Date(event_date). The proper comparison is

new Date(event_date) <= new Date()

const arr = [{
        "id": "e18dae03-99c7-4ffa-be2f-a595fc7fad15",
        "event_date": "2021-09-30T01:00:00.000Z",
        "alert_name": null,
        "alert_level": null,
        "alert_action": null,
        "alert_details": null,
    },
    {
        "id": "e18dae03-99c7-4ffa-be2f-a595fc7fad15",
        "event_date": "2021-10-30T01:00:00.000Z",
        "alert_name": null,
        "alert_level": null,
        "alert_action": null,
        "alert_details": null,
    },
    {
        "id": "765a508f-fb47-4de4-bf69-19f0db38ccfc",
        "event_date": "2021-09-17T22:03:00.000Z",
        "alert_name": null,
        "alert_level": null,
        "alert_action": null,
        "alert_details": null,
    },
    {
        "id": "bc07a114-43f4-49a1-bb6c-fec801aee749",
        "longitude": null,
        "event_date": "2021-11-19T07:51:00.000Z",
        "alert_name": null,
        "alert_level": null,
        "alert_action": null,
        "alert_details": null,
    }
]


const filterByExpiration = () => arr.filter(({ event_date }) => new Date(event_date) <= new Date());

console.log(filterByExpiration());

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!