Method with for loop and if statement not working

pickByTime(time) {
    
    const movieOptions = [];
    
    for (let i = 0; i <= this.genres.horror.length; i++) {  
             if(time === this.genres.horror[i][1]) {
                  movieOptions.push(this.genres.horror[i][0])
      }
    }
    return movieOptions
  }

I am going to try to explain this accurately. I may use the wrong nomenclature here and there so feel free to holler/berate me as needed. Above is a method inside of an object called ‘list’. I am trying to have it iterate through each horror movie listed, check to see if the time argument passed through equals the value in this.genres.horror[I][1], and if it does then push the name of the movie which is in this.genres.horror[I][0] to the movieOptions array.

When I call upon it with list.pickByTime(‘evening’) I get the following error:

if(time === this.genres.horror[i][1]) {                                     
TypeError: Cannot read property '1' of undefined
    at Object.pickByTime 

I am rather confused because when I check this.genre.horror[0][1], for example, it returns ‘evening’. I am not understanding how 1 can be undefined in this scenario.

Any thoughts?

Hi,
I think it’s rather simple: I think that just the last element this.genres.horror[this.genres.horror.length] is undefined.
Let’s say you have 5 movies in the horror genre. That means this.genres.horror[0] is the first and this.genres.horror[4] is the last.
But you loop stops at element 5 (i <= this.genres.horror.length). That means the last element would be this.genres.horror[5] and that doesn’t exist.