[英]Show at least one online item from array
I have an array of users which i have sorted by distance. 我有一组用户,我按距离排序。 I want to display the 5 nearest items but sometimes the 5 nearest items all have "offline" state, in which case i want to ensure at least 1 "online" item is added also.
我想显示最近的5个项目,但有时候最近的5个项目都有“离线”状态,在这种情况下我想确保至少添加1个“在线”项目。
Here is my attempt so far which isn't quite working. 这是我到目前为止的尝试,但并不是很有效。 It didn't show the 5 "offline" items plus 1 "online" item like i wanted, it showed all.
它没有显示5个“离线”项目加上我想要的1个“在线”项目,它显示了全部。
var items = 0;
var online = usersOnline();
var itemsOnline = 0;
for (var i = 0; i < nearest.length; i++) {
var item = nearest[i];
if ( items < 5 ) {
items++;
if ( item.state != "offline" ) { itemsOnline++; }
jQuery( "#leaflet-control-geosearch-results" ).append( "<li id='record-"+item.label+"'><label><input type='checkbox' class='cb' value='"+item.id+"'> <img src='img/presence/"+item.state+"_map.png' height='10px'> "+item.label+" ("+parseFloat(item.distance / 1000).toFixed(2)+strings[strLang].nearestMetre+")</label><div class='icons'><a title='"+strings[strLang].nearestLocate+"' class='locate' href='"+item.label+"'><img src='img/icons/locate.png' height='15px' /></a> <a title='"+strings[strLang].routeGet+"' class='directions' href='"+item.label+"'><img src='img/icons/directions.png' height='15px' /></a></div><div class='clear'></div></li>" );
}
}
while (( online > 0 ) && ( itemsOnline < 1 )) {
for (var i = 0; i < nearest.length; i++) {
var item = nearest[i];
if ( item.state != "offline" ) {
itemsOnline++; }
jQuery( "#leaflet-control-geosearch-results" ).append( "<li id='record-"+item.label+"'><label><input type='checkbox' class='cb' value='"+item.id+"'> <img src='img/presence/"+item.state+"_map.png' height='10px'> "+item.label+" ("+parseFloat(item.distance / 1000).toFixed(2)+strings[strLang].nearestMetre+")</label><div class='icons'><a title='"+strings[strLang].nearestLocate+"' class='locate' href='"+item.label+"'><img src='img/icons/locate.png' height='15px' /></a> <a title='"+strings[strLang].routeGet+"' class='directions' href='"+item.label+"'><img src='img/icons/directions.png' height='15px' /></a></div><div class='clear'></div></li>" );
}
}
}
You should be able to do the whole thing with just one pass/loop: 你应该只用一个pass / loop来完成整个事情:
Try: 尝试:
var items = 0;
var online = usersOnline();
var itemsOnline = 0;
for (var i = 0; i < nearest.length; i++) {
var item = nearest[i];
if (item.state != "offline") {
itemsOnline++;
} else if (items >= 5) {
// we already have 5 items, so we're now only interested in online items. this new one is an offline one, so skip it
continue;
}
items++;
jQuery( "#leaflet-control-geosearch-results" ).append( "<li id='record-"+item.label+"'><label><input type='checkbox' class='cb' value='"+item.id+"'> <img src='img/presence/"+item.state+"_map.png' height='10px'> "+item.label+" ("+parseFloat(item.distance / 1000).toFixed(2)+strings[strLang].nearestMetre+")</label><div class='icons'><a title='"+strings[strLang].nearestLocate+"' class='locate' href='"+item.label+"'><img src='img/icons/locate.png' height='15px' /></a> <a title='"+strings[strLang].routeGet+"' class='directions' href='"+item.label+"'><img src='img/icons/directions.png' height='15px' /></a></div><div class='clear'></div></li>" );
if (items >= 5 && itemsOnline >= 1) {
// we've now got at least 5 items, and at least 1 is online, so we're done.
break;
}
}
Your while loops condition only gets evaluated after the inner for loop has completed iterating through the entire nearest
array. 只有在内部for循环完成迭代遍历整个
nearest
数组之后,才会对while循环条件进行求值。
Since you only need to itterate the through the nearest
array once to get an online item, you can replace the while loop with an if statement: 由于您只需要通过
nearest
数组迭代一次以获取在线项目,因此可以使用if语句替换while循环:
if (itemsOnline < 1) {
for (var i = 5; i < nearest.length; i++) { // start from 6th element
var item = nearest[i];
if (item.state != "offline") {
jQuery("#leaflet-control-geosearch-results").append(etc);
break; // exit loop
}
}
}
var items = 0;
var online = usersOnline();
var itemsOnline = 0;
for ( var i = 0; i < nearest.length && items < 5; i++, items++ ) {
var item = nearest[i];
items++;
if ( item.state != "offline" ) { itemsOnline++; }
apendItem( item );
}
for ( var i = 5; i < nearest.length && itemsOnline < 1; i++ ) {
var item = nearest[i];
apendItem( item );
break;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.