Home > OS >  Problems with Array.Find method with Dates in Typescript
Problems with Array.Find method with Dates in Typescript

Time:11-11

I manage an array of Objects like this

enter image description here

I see that this is the same

enter image description here

But when I try this

enter image description here

Any idea, please?

Thanks

CodePudding user response:

I don't think it has anything to do with TypeScript or the find method.

If I try to print the result of

new Date("2022-10-12T00:00:00") == new Date(2022, 9, 12)

I get false, so it's normal that the find returns undefined.

Maybe you should try to compare stringified Dates (using methods such as toISOString()) instead of Dates.

CodePudding user response:

Try

this.festivos.find(f => new Date(f.fetcha).getTime()==new Date(2022,9,12).getTime())

When you compare two Date objects, you fall victim to object reference equality. You could compare their time component instead as illustrated above.

  • Related