简体   繁体   English

在数组中显示至少一个在线项目

[英]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+"'>&nbsp;<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>&nbsp;<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+"'>&nbsp;<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>&nbsp;<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+"'>&nbsp;<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>&nbsp;<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.

相关问题 如何在Angular JS中一一显示json数组中的项目 - How to show item from json Array one by one in Angular JS 如何从给定的项目数组中找到其数据与至少一个项目的条件匹配的文档 - mongoDB - How to find documents that their data match a condition on at least one item from a given array of items - mongoDB 是的验证——确保一个数组至少包含一项 - Yup validation -- making sure an array contains at least one item 当单击另一个数组中的另一个单独的项目,同时又从第一个数组中删除该项目时,从一个数组中随机显示项目? - Show item at random from one array when another separate item in a different array is clicked while also removing that item from the first array? Selectize.js-从要选择的选择中至少执行一项 - Selectize.js - Enforce At Least One Item From Select To Be Picked 显示数组中的特定项目? - Show A Specific Item From Array? 映射数组中的一项 - Map one item from array 如何检查数组的至少一项是否包含在 object 的数组中并遍历所有对象(JS) - How to check if at least one item of an array is included in an array of an object and loop through all objects (JS) 使用$ http.get从json文件显示一个数组项 - Show one array item from json file using $http.get 将数组中的一项附加到一个 div - Append one item from an array to one div
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM