2

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;  

}
2
  • Can you post some of your data/data structures in driverLapNumber array and driverLapTimes? Commented Sep 28, 2016 at 21:12
  • Did you initialize driverLapNumber[999] as an Array? driverLapNumber[999] = new Array() Commented Sep 28, 2016 at 21:15

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

4 Comments

This immediately looked logical and yes it worked, many thanks!
Firstly: I'll edit in a quick fix that checks to see if there's something in there. 2nd : If I'm getting you right, then you have values that were in there before, but are cleared from the loop afterward? If that's true, you should consider creating a method that constructs your array before hand, and doesn't declare the 2nd dimension of the loop above.
Perfect, many thanks, my jQuery is rusty, and because the script is not going to be that big I am happy with a simple fix :)
Happy to help. I know it probably doesn't matter. But since a lap time of 0 is falsey, it will reinitialize the array if a value of 0 is in there, so you'll get an undefined instead of a 0 for the laptime. Just sayin.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.