【发布时间】:2017-02-07 00:49:21
【问题描述】:
感谢 Chad 的解决方案,但它现在似乎从数组中清除了值,这是控制台日志上的 foreach,它向您显示了我的情况(随后是更新功能的更新代码):
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>");
}
我想我可以把这归咎于 PHP,因为我目前编写它的方式是可能的,我需要帮助来纠正这个问题。
除了driverLapTimes 数组上的 SECOND 键外,一切都很好,我希望它输出如下内容:
driverLapNumber[999][1] = 6.666;
driverLapNumber[999][2] = 6.666;
driverLapNumber[999][3] = 6.666;
driverLapNumber[999][4] = 6.666;
但是 1,2,3,4 键出现以下控制台错误:
未捕获的类型错误:无法设置未定义的属性“1”
功能代码:
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;
}
【问题讨论】:
-
您可以在 driverLapNumber 数组和 driverLapTimes 中发布您的一些数据/数据结构吗?
-
您是否将 driverLapNumber[999] 初始化为数组? driverLapNumber[999] = new Array()
标签: javascript jquery arrays multidimensional-array