jQuery多维数组(动态键) - 无法设置undefined的属性 [英] jQuery Multidimensional Array (dynamic key) - Cannot set property of undefined
问题描述
感谢Chad的解决方案,但现在它似乎清除了数组中的值,这是控制台日志中的 foreach
,它显示了我的情况(其次是更新函数的更新代码):
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假设我可以为此归咎于PHP,因为它可以用我当前的写作方式,我需要帮助纠正这个。
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.
一切都很好,除了 driverLapTimes
数组中的第二个键,我希望它输出如下内容:
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;
但1,2,3,4键出现以下控制台错误:
But the 1,2,3,4 keys are coming up with the following console error:
未捕获的TypeError:无法设置未定义的属性'1'
Uncaught TypeError: Cannot set property '1' of undefined
功能代码:
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;
}
推荐答案
在此例如,数组项可能尚未声明为新数组。试试这个:
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.
这篇关于jQuery多维数组(动态键) - 无法设置undefined的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!