prashant padadune
prashant padadune

Reputation: 151

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());



Upvotes: 0

Views: 1794

Answers (4)

dork
dork

Reputation: 4568

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());

Upvotes: 3

Nitheesh
Nitheesh

Reputation: 19986

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())

Upvotes: 1

Tushar Shahi
Tushar Shahi

Reputation: 20441

Your event_date is a string. That is why the comparison is giving results you do not expect. You need to convert the string into a Date object.

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 }) => {
let x = new Date(event_date);
return (x <= new Date());
});

console.log(filterByExpiration());

Upvotes: 1

Newbie
Newbie

Reputation: 4819

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());

Upvotes: 1

Related Questions