jQuery Multidimensional Array (dynamic key) - Cannot set property of undefined

后端 未结 2 1093
时光说笑
时光说笑 2021-01-22 01:15

Thanks you Chad for his solution, however it now appears to clear values from the array, here is a foreach on the console log which shows you my situation (followed

2条回答
  •  时光说笑
    2021-01-22 01:57

    In this case it's likely that the array item hasn't been declared as a new array. Try this:

    function updateLap(kartId){
    
    
    
    if (isNaN(driverLapNumber[kartId])) {
         window.driverLapNumber[kartId] = 0;  
      }
    
      //ADD ONE LAP TO THE KART ID
      driverLapNumber[kartId]++;
    
      //ADD LAP TIME FOR CURRENT LAP NUMBER
      if(!driverLapTimes[kartId]){
           driverLapTimes[kartId] = [];
      }
      driverLapTimes[kartId][driverLapNumber[kartId]] = window.lapTime;  
    
    }
    

    Of course you could always put the declaration outside of this loop by creating a method to construct your array before hand.

提交回复
热议问题