jQuery Multidimensional Array (dynamic key)

Question!

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 by the updated code for the update function):

timer.html:60 ------------------------------------
timer.html:57 0
timer.html:58 undefined
timer.html:57 1
timer.html:58 1.910
2timer.html:60 ------------------------------------
timer.html:57 0
timer.html:58 undefined
timer.html:57 1
timer.html:58 undefined
timer.html:57 2
timer.html:58 1.727
2timer.html:60 ------------------------------------
timer.html:57 0
timer.html:58 undefined
timer.html:57 1
timer.html:58 undefined
timer.html:57 2
timer.html:58 undefined
timer.html:57 3
timer.html:58 0.690
timer.html:60 ------------------------------------

=============================================

function updateLap(kartId){

  if (isNaN(driverLapNumber[kartId])) {
     //IF LAP NOT SET THEN THE KART ID NEEDS TO BE SET AS 0 AS IT IS THE START OF THE RACE
     window.driverLapNumber[kartId] = 0;  
  }

  //ADD ONE LAP TO THE KART ID
  driverLapNumber[kartId]++;

  //ADD LAP TIME FOR CURRENT LAP NUMBER  
  driverLapTimes[kartId] = [];
  driverLapTimes[kartId][driverLapNumber[kartId]] = window.lapTime;

   $.each(driverLapTimes , function( index, obj ) {
    $.each(obj, function( key, value ) {
        console.log(key);
        console.log(value);     
    });
    console.log("------------------------------------");
  });


  $(".lapTimes").prepend("kartId: "+kartId+" - "+window.lapTime+"<br>");

}

I suppose I can blame PHP for this as it would be possible with the way I am writing it currently, I need assistance in correcting this please.

Everything is fine except for the SECOND key on the driverLapTimes array, I want it to output something like:

driverLapNumber[999][1] = 6.666;
driverLapNumber[999][2] = 6.666;
driverLapNumber[999][3] = 6.666;
driverLapNumber[999][4] = 6.666;

But the 1,2,3,4 keys are coming up with the following console error:

Uncaught TypeError: Cannot set property '1' of undefined

The function code:

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
  driverLapTimes[kartId][driverLapNumber[kartId]] = window.lapTime;  

}


Answers

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.



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]);



The key word that you're apparently missing is "epsilon." If you search on Python epsilon value then it returns a links to StackOverflow: Value for epsilon in Python near the top of the results in Google.

Obviously it helps if you understand how the term is associated with this concept.



This video can help you solving your question :)
By: admin