我不知道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