我有一个认为1>的for循环2,走出它的循环。它应该将JSON根对象放入带有select
标记的HTML option
中。它必须显示两个选项,但它只在我的代码中显示一个。这是JSON:
[{
"batch": {
"batch_id": 0,
"questions": [{
"name": ["Communicatie"],
"question_id": 25,
"mark": ["9", "7", "10"]
}, {
"name": ["Samenwerking"],
"question_id": 27,
"mark": ["9", "8", "7"]
}, {
"name": ["Dienstverlening"],
"question_id": 30,
"mark": ["9", "2", "5"]
}, {
"name": ["Facturatie"],
"question_id": 28,
"mark": ["8", "7", "5"]
}, {
"name": ["Technische kennis"],
"question_id": 22,
"mark": ["9", "8", "7"]
}, {
"name": ["Distributieproces"],
"question_id": 29,
"mark": ["4", "7", "5"]
}, {
"name": ["Overleg"],
"question_id": 24,
"mark": ["6", "8", "7"]
}, {
"name": ["Markt kennis"],
"question_id": 23,
"mark": ["6", "7"]
}, {
"name": ["Flexabiliteit"],
"question_id": 26,
"mark": ["3", "6", "7"]
}]
}
}, {
"batch": {
"batch_id": 1,
"questions": [{
"name": ["Communicatie"],
"question_id": 25,
"mark": ["7", "2"]
}, {
"name": ["Samenwerking"],
"question_id": 27,
"mark": ["4", "7"]
}, {
"name": ["Dienstverlening"],
"question_id": 30,
"mark": ["6", "4"]
}, {
"name": ["Facturatie"],
"question_id": 28,
"mark": ["8", "3"]
}, {
"name": ["Technische kennis"],
"question_id": 22,
"mark": ["6"]
}, {
"name": ["Distributieproces"],
"question_id": 29,
"mark": ["3", "7"]
}, {
"name": ["Overleg"],
"question_id": 24,
"mark": ["8", "4"]
}, {
"name": ["Markt kennis"],
"question_id": 23,
"mark": ["3", "7"]
}, {
"name": ["Flexabiliteit"],
"question_id": 26,
"mark": ["6", "2"]
}]
}
}];
我已经隔离了一小段代码here。幸运的是,代码的这个特定部分可以工作,所以这不是问题,但我只是想让你知道我想用代码做什么。
以下是我的完整代码及评论:
$(document).ready(function(){
$.getJSON("/rest/BatchService/batches", function(json)
{
//json.length = 2, json is an arraylist of jsonobjects
for(var i = 0; i < json.length; i++){
//get batchid
batchid = json[i]["batch"]["batch_id"];
//put batchids into an option with value batchid tag and put it in select tag
var option=$('<option />').val(json[i]["batch"]["batch_id"]).text("Batch " +json[i]["batch"]["batch_id"]);
$("#dropdownBatches").append(option);
//get from 'questions' the name and marks and put it in vars so that I can later put it in ChartJS
var questions = json[i]["batch"]["questions"];
for(var i in questions){
var name= questions[i]["name"];
chartjslabels.push(name);
console.log(name);
var marks = questions[i]["mark"];
var sum = 0;
for(var i = 0; i < marks.length; i++){
sum+= parseInt(marks[i]);
}
var avg = sum/marks.length;
chartjsdata.push(avg);
}
}
其他一些信息:
我到处都做了断点,并且在最后一个大括号中,它假设进入下一次迭代,它停止并转到我的ChartJS来制作图表。
总而言之,我的代码中存在一个隐藏的中断,它会停止for循环。我无法找到它。
答案 0 :(得分:4)
因为你为每个循环使用相同的变量i
,所以在这里使用不同的增量[i,j,z]
$(document).ready(function(){
$.getJSON("/rest/BatchService/batches", function(json)
{
//json.length = 2, json is an arraylist of jsonobjects
for(var i = 0; i < json.length; i++){
//get batchid
batchid = json[i]["batch"]["batch_id"];
//put batchids into an option tag and put it in select tag
var option=$('<option />').val(json[i]["batch"]["batch_id"]).text("Batch " +json[i]["batch"]["batch_id"]);
$("#dropdownBatches").append(option);
//get from 'questions' the name and marks and put the variables in a chart
var questions = json[i]["batch"]["questions"];
for(var j in questions){
var name= questions[j]["name"];
chartjslabels.push(name);
console.log(name);
var marks = questions[j]["mark"];
var sum = 0;
for(var z = 0; z < marks.length; z++){
sum+= parseInt(marks[z]);
}
var avg = sum/marks.length;
chartjsdata.push(avg);
}
}
答案 1 :(得分:1)
您正在使用相同的变量i,将内循环更改为其他
for(var j in questions){
var name= questions[j]["name"];
chartjslabels.push(name);
console.log(name);
var marks = questions[j]["mark"];
var sum = 0;
for(var k = 0; i < marks.length; i++){
sum+= parseInt(marks[k]);
}
var avg = sum/marks.length;
chartjsdata.push(avg);
}
答案 2 :(得分:1)
这里的问题非常简单,你在3个嵌套for循环中使用i变量,所以它的值会多次递增,导致父for循环只运行一次。
答案 3 :(得分:0)
for(var i in questions){
for(var i = 0; i < marks.length; i++){
对两个循环使用i
会导致您看到的行为。
答案 4 :(得分:0)
您不能在嵌套的for循环中使用相同的变量名。这意味着i-var将在所有循环中递增,这意味着代码将不会按照您的意愿执行。
将变量名称更改为其他字母以解决问题。