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

后端 未结 2 1094
时光说笑
时光说笑 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.

    0 讨论(0)
  • 2021-01-22 02:01

    Try as below and let me know

    var a = new Array();
    a[0]= new Array();
    a[0].push(1);
    a[0].push(2);
    a[0].push(3);
    
    console.log(a[0][0]);
    console.log(a[0][1]);
    console.log(a[0][2]);

    0 讨论(0)
提交回复
热议问题