[英]My javascript for loop is only printing 1 rows out of an array with 3 rows
我不知道在for循环中正确的条件应该是什么,该循环遍历一个数组,该数组的长度根据我要从中添加并添加到该数组的表中的行数而变化。 该数组当前有三行,但是for循环仅打印出两行。 我的代码:
//my database query
query.find({
success: function(results) {
//this for loop works fine
for (i = 0; i < results.length; i++){
eventID = results[i].id;
activity = results[i].get("activity");
scene = results[i].get("location");
neighborhood = results[i].get("neighborhood");
date = results[i].get("date");
details = results[i].get("details");
time = results[i].get("time");
objIDs.push(eventID);
//each row gets pushed into an array
search.push([activity, scene, neighborhood, date, details, time]);
//I empty a div on a page that uses the ajax load() method to load an html page.I replace that html with the array of query results.
$('#div1').empty();
//there are currently 3 rows in my array, but when I loop through it and append() the </br>rows to the div, only one gets printed. I've tried changing the comparison operator to </br>different things but nothing works. I'm definitely getting all the rows from the query because when I alert() the search array I see all the rows.
for (i = 0; i <= search.length; i++) {
$('#mainDiv').append("<div id='event'>" + search[i].join(' ') + "<a href='#' class='interested'>Interested?</a></div>");
}
};// closes for
},//closes success
error: function(error) {
alert("Error: " + error.code + " " + error.message);
}
}); //closes find
虽然下面是一个问题,这可能不是问题。 但是,由于我无法删除此帖子(我必须登录或进行其他操作?),因此它仍为工件。 即使问题是其他原因,也请注意。
该代码在嵌套循环中使用相同的i
变量-并在两个循环中递增i
。
使用其他变量。
您for
内部有一个for
, for
您对其进行迭代时尚未完全初始化搜索。
};// closes for
for (i = 0; i <= search.length; i++) {
$('#mainDiv').append("<div id='event'>" + search[i].join(' ') + "<a href='#' class='interested'>Interested?</a></div>");
}
},//closes success
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.