0

I am trying to do something like this but it returns -1 all the time even if it should return an index:

let f = this.events.findIndex(e => e.endTime == this.dataService.yearlyTimeSlots[i].From);

Am I doing something wrong?

5
  • 2
    Am I doing something wrong? Yes, you're not providing enough detail. What is the value of this.events.findIndex? What value is passed to e? Commented Jun 7, 2017 at 9:48
  • 1
    -1 means it was not found. That means that there is no element in this.events whose endTime property is equal to this.dataService.yearlyTimeSlots[i].From. Commented Jun 7, 2017 at 9:50
  • events is an array of objects, e.endTime is a date and this.dataService.yearlyTimeSlots[i].From is a date too, the method itself returns -1 all the time Commented Jun 7, 2017 at 9:53
  • You need to make sure that the dates you are trying to compare are in precisely the same format, such as msec since epoch, or ISO format, etc. This should not be that hard to debug. Stop the debugger at that line, and examine this.events and this.dataService.yearlyTimeSlots[i], and make sure that the latter value occurs as the endTime property of some element in this.events. Just out of curiosity, where is i coming from? Commented Jun 7, 2017 at 9:56
  • it is index of for loop Commented Jun 7, 2017 at 10:00

0