昨天我做了这个小功能,工作正常。今天早上我检查它,它不再工作了。我不明白为什么。
代码在第一次调用'var color = dataPHP [i] ['color_code']时给出了错误;'
TypeError:'undefined'不是对象(评估'dataPHP [i] ['color_code']')全局代码
var color_code_hold = [];
for (ix = 0; ix <= 5; ++ix){
for (i = 0; dataPHP.length; ++i) {
var color = dataPHP[i]['color_code'];
if(color === ix){
// alert('found ix=='+ix+'color==='+color);
color_code_hold[color_code_hold.length] = color;
break;
}
if(dataPHP.length >= 20){
var check_length = dataPHP.length-4;
}else{
var check_length = dataPHP.length;
}
if(i >= check_length){
// omit ix
// alert('broke'+ix);
break;
}
}
}
var total_value = 0;
for(i = 0; i < color_code_hold.length; ++i){
var cur_color_h = color_code_hold[i];// held value
// alert('cur_color_h val'+cur_color_h);
if(cur_color_h === 0){
total_value = total_value+25;
// alert('added 0');
}else
if(cur_color_h === 1){
total_value = total_value+25;
// alert('added 1');
}else
if(cur_color_h === 2){
total_value = total_value+20;
// alert('added 2');
}else
if(cur_color_h === 3){
total_value = total_value+15;
// alert('added 3');
}else
if(cur_color_h === 4){
total_value = total_value+10;
// alert('added 4');
}else
if(cur_color_h === 5){
total_value = total_value+5;
// alert('added 5');
}
}
math_rand_num = Math.floor((Math.random() * total_value) + 1);
color_code_hold.sort(function(a, b){return a-b});
first_add_value = 0;
for(i = 0; i < color_code_hold.length; ++i){
var cur_color = color_code_hold[i];
if(cur_color === 0){
second_add_value = first_add_value + 25;
if(math_rand_num <= second_add_value){
var select_color = 0;
break;
}else{
first_add_value = second_add_value;
}
}
if(cur_color === 1){
second_add_value = first_add_value + 25;
if(math_rand_num <= second_add_value){
var select_color = 1;
break;
}else{
first_add_value = second_add_value;
}
}
if(cur_color === 2){
second_add_value = first_add_value + 20;
if(math_rand_num <= second_add_value){
var select_color = 2;
break;
}else{
first_add_value = second_add_value;
}
}
if(cur_color === 3){
second_add_value = first_add_value + 15;
if(math_rand_num <= second_add_value){
var select_color = 3;
break;
}else{
first_add_value = second_add_value;
}
}
if(cur_color === 4){
second_add_value = first_add_value + 10;
if(math_rand_num <= second_add_value){
var select_color = 4;
break;
}else{
first_add_value = second_add_value;
}
}
if(cur_color === 5){
second_add_value = first_add_value + 5;
if(math_rand_num <= second_add_value){
var select_color = 5;
break;
}else{
first_add_value = second_add_value;
}
}
}
for (i = 0; i < dataPHP.length; ++i) {
var color = dataPHP[i]['color_code'];
if(color === select_color){
var key = i;
break;
}
}
我输入了一小段代码来检查它,并且它在先前声明的行上出现了错误。
var i = 0;
var color = dataPHP[i]['color_code'];
alert(color);
我真的不知道为什么它不起作用。你能不把某些变量放在for循环中吗?它怎么可能在昨天工作并且今天神秘地破裂?
此外,这是一个小数组样本。
var dataPHP = [
{"deck_id":"1001","card_key":"1005","front_side":"s1","back_side":"s2","card_three":"s3","last_seen":0,"color_code":0,"last_study":0,"placement":1},
{"deck_id":"1001","card_key":"1004","front_side":"a2","back_side":"a1","card_three":"a3","last_seen":0,"color_code":5,"last_study":1414769400,"placement":2}];
答案 0 :(得分:2)
假设首先定义dataPHP
。我没有看到您的测试用例存在问题。虽然注意你的for循环在这种情况下有问题:
for (i = 0; dataPHP.length; ++i) {
需要:
for (i = 0; i < dataPHP.length; ++i) {
i
必须大于dataPHP.length
,导致无限循环错误输出。
<强> Here is an example 强>