JavaScript-“变量未定义"在循环的一部分中,在另一部分中工作 [英] JavaScript - "variable undefined" in one part of loop, working in a different part
问题描述
使用以下代码,出现此错误RewardPurchases.PurchasesArray[i].Student_Name is undefined
:
Using the following code, I'm getting this error RewardPurchases.PurchasesArray[i].Student_Name is undefined
:
$('button#random').click( function() {
var Num = Math.floor(Math.random() * Total+1);
Num--;
for (var i in RewardPurchases.PurchasesArray) {
/* --------> */
$('#display').text("Ticker number " + Num + " in the LEAP database belongs to...\n\n\n" + RewardPurchases.PurchasesArray[i].Student_Name.toUpperCase() + " (" + TutorGroup + ")").show().delay(300);
if (i == Num) {
var TutorGroup = '';
Frog.API.get('timetable.getClasses',
{
'params': {'student': RewardPurchases.PurchasesArray[i].Student_ID },
'onSuccess': function(data) {
for (var i = 0; i < data.length; i++) {
if (data[i].subject.name == "Tut Period") {
TutorGroup = data[i].name.replace("/Tp", "");
}
}
}
});
$('#display').animate({'font-size': 36}, 1500, function() {
$(this).prepend('<p>!!! WINNER !!!</p>');
});
alert("Ticker number " + Num + " in the LEAP database belongs to...\n\n\n" + RewardPurchases.PurchasesArray[i].Student_Name.toUpperCase() + " (" + TutorGroup + ")");
}
}
} );
但是,如果我按如下所示移动此行$('#display').text(...
,该错误就会消失:
However, if I move this line $('#display').text(...
as follows, the error disappears:
$('button#random').click( function() {
var Num = Math.floor(Math.random() * Total+1);
Num--;
for (var i in RewardPurchases.PurchasesArray) {
if (i == Num) {
var TutorGroup = '';
/* --------> */
$('#display').text("Ticker number " + Num + " in the LEAP database belongs to...\n\n\n" + RewardPurchases.PurchasesArray[i].Student_Name.toUpperCase() + " (" + TutorGroup + ")").show().delay(300);
Frog.API.get('timetable.getClasses',
{
'params': {'student': RewardPurchases.PurchasesArray[i].Student_ID },
'onSuccess': function(data) {
for (var i = 0; i < data.length; i++) {
if (data[i].subject.name == "Tut Period") {
TutorGroup = data[i].name.replace("/Tp", "");
}
}
}
});
$('#display').animate({'font-size': 36}, 1500, function() {
$(this).prepend('<p>!!! WINNER !!!</p>');
});
alert("Ticker number " + Num + " in the LEAP database belongs to...\n\n\n" + RewardPurchases.PurchasesArray[i].Student_Name.toUpperCase() + " (" + TutorGroup + ")");
}
}
} );
我不明白为什么会这样? i
语句未定义i
吗?
I don't understand why this is the case? i
isn't being defined by the if
statement?
我正在尝试在数组中显示每个名称,然后选择一个随机名称并使用"WINNER!"将其显示为大字体.在它上面.
I'm trying to display each name in an array, before picking a random name and displaying it in a large font with "WINNER!" above it.
预先感谢
推荐答案
您的数组的完整性如何?
What is the integrity of your array like?
//RewardPurchases.PurchasesArray
[0] undefined
[1] { Student_Name: undefined }
[2] { Student_Name: 'Bob' }
以上内容在数组中均有效. [0]
和[1]
都会给您您收到的错误.
The above are all valid in an array. [0]
and [1]
will both give you the error you received.
如果PurchasesArray不是数组而是对象,那么您需要在循环内部进行检查.
If PurchasesArray is not an array but an object - then you need to do a check inside your loop.
for (var i in RewardPurchases.PurchasesArray) {
if(!RewardPurchases.PurchasesArray.hasOwnProperty(i)) {
continue;
}
//rest of code...
}
这篇关于JavaScript-“变量未定义"在循环的一部分中,在另一部分中工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!