根据键值优先处理JavaScript数组

时间:2017-04-07 11:10:40

标签: javascript jquery jquery-ui autocomplete jquery-ui-autocomplete

我有一个Javascript数组

var airports = [
{
    iata: "CPT",
    city: "Cape Town",
    airport: "Cape Town International",
    country: "South Africa",
    priority: 9
},

{
    iata: "HLA",
    city: "Johannesburg",
    airport: "Lanseria",
    country: "South Africa",
    priority: 1
},

{
    iata: "JNB",
    city: "Johannesburg",
    airport: "OR Tambo International",
    country: "South Africa",
    priority: 9
},
];

请注意:最终的.js文件已列出超过3 000个机场。

我正在尝试使用自动填充功能返回优先结果。

在上面的示例中,如果用户开始键入“约翰内斯堡”,则必须根据“优先级”值进行优先级排序,例如JNB,OR Tambo International应始终显示在HLA,Lanseria之上。

目前,自动填充根据数组中列出的位置显示结果。

这里可以找到一个小提琴: https://jsfiddle.net/cgaybba/17p7uyvf/

2 个答案:

答案 0 :(得分:0)

您可以使用自定义比较功能对$.grep的结果进行排序:

let temp = $.grep(airportArray, function(value)  
    return matcher.test(value.label) || matcher.test(value.value) || matcher.test(value.airport) || matcher.test(value.country);
}

response(temp.sort((airportA, airportB) => {
    if(airportA.priority < airportB.priority){
        return -1;
    }
    else if(airportA.priority > airportB.priority){
        return 1;
    }
    else{
        return 0;
    }
}));

答案 1 :(得分:0)

尝试此代码可能对您有用

var airportArray = [
    {
        iata: "CPT",
        city: "Cape Town",
        airport: "Cape Town International",
        country: "South Africa",
        priority: 9
    },

    {
        iata: "HLA",
        city: "Johannesburg",
        airport: "Lanseria",
        country: "South Africa",
        priority: 1
    },

    {
        iata: "JNB",
        city: "Johannesburg",
        airport: "OR Tambo International",
        country: "South Africa",
        priority: 9
    },

    {
        iata: "DUR",
        city: "Durban",
        airport: "King Shaka International",
        country: "South Africa",
        priority: 9
    },

    {
        iata: "BFN",
        city: "Bloemfontein",
        airport: "Bram Fischer International",
        country: "South Africa",
        priority: 9
    },

    {
        iata: "PLZ",
        city: "Port Elizabeth",
        airport: "Port Elizabeth Airport",
        country: "South Africa",
        priority: 8
    },

    {
        iata: "AGZ",
        city: "Aggeneys",
        airport: "Aggeneys Airport",
        country: "South Africa",
        priority: 6
    },

    {
        iata: "GRJ",
        city: "George",
        airport: "George Airport",
        country: "South Africa",
        priority: 7
    },

    {
        iata: "ELS",
        city: "East London",
        airport: "Ben Schoeman Airport",
        country: "South Africa",
        priority: 7
    }

];
 var airportArray = airportArray.sort(function(a, b) {                  
                return (b.priority || 0) - (a.priority || 0);
            });   
$(function() {

    function custom_source(request, response) {
        var matcher = new RegExp($.ui.autocomplete.escapeRegex(request.term), "i");
        response($.grep(airportArray, function(value) {
            return matcher.test(value.label) || matcher.test(value.value) || matcher.test(value.airport) || matcher.test(value.country);
        }));
    }

    $("#input_1_1").autocomplete({
        minLength: 3,
        source: custom_source,
        ookupLimit: 3,
        clearCache: true,
        sortBeforeLimit: function(suggestions) {
            var sorted = suggestions.sort(function(a, b) {
                    alert(a.priority);
                return (a.priority || 0) - (b.priority || 0);
            });
            return sorted;
        },
        focus: function(event, ui) {
            // Text inside INPUT while selecting airport
            $("#input_1_1").val(ui.item.city + ', ' + ui.item.airport + ' (' + ui.item.iata + '), ' + ui.item.country);
            return false;
        },
        select: function(event, ui) {
            // Text inside INPUT AFTER selecting
            $("#input_1_1").val(ui.item.city + ' (' + ui.item.iata + ')');
            $("#input_1_2").val(ui.item.iata);
            return false;
        }
    })

        .autocomplete("instance")._renderItem = function(ul, item) {
        return $('<li>')
            .append('<div class="result-row">' + item.city + ', ' + item.airport + ' (' + item.iata + '), ' + item.country + '</div>')
            .appendTo(ul);
    };
});