如何排序数组或正确显示?

时间:2017-05-11 07:17:23

标签: javascript jquery

有这个json并取值:

[[name, brand, oem, category], [name, brand, oem, category], [name, brand, oem, category], [name, brand, oem, category]]

我的js

$(function(){
$('input[name="oem"]').autoComplete({
    minChars: 4,
    // parse json and output the values 11
    source: function(term, response) {
        term = term.toLowerCase();
        $.getJSON('/search.json?oem='+ term, function (data) {
            var matches = [];
            for (i = 0; i < data.length; i++)
                if (~data[i][0].toLowerCase().indexOf(term)) matches.push(data[i]);
            response(matches.slice(0,11));
        });
    },
    // how to display
    renderItem: function (item, search){
        search = search.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&');
        var re = new RegExp("(" + search.split(' ').join('|') + ")", "gi");
        return '<div class="autocomplete-suggestion" data-category="' + item[3] + '">' + '<font color="bbbbbb" style="font-style: italic">' + 'Категория, ' + '</font>'+ item[3].replace(re, "<b>$1</b>") + '</div><div class="autocomplete-suggestion" data-detail="' + item[0] + '" data-make="' + item[1] + '" data-oem="' + item[1] + '">' + item[0].replace(re, "<b>$1</b>") + '</div>'; 
        // here put first the category (item[3]), then all the other elements. ITEM is the elements of the array
       },
    });
 });

item包含[name, brand, oem, category]

显示:

Category
Name
Category
Name
Category
Name
Category
Name

你需要得到:

Category
Category
Category
Name
Name
Name
Name
Name

试图通过sort来完成。但我认为需要不使用数组。可以使用append

搜索自动填充功能。使用此plug-in

UPD:

使用追加实施

renderItem: function (item, search){
            search = search.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&');
            var re = new RegExp("(" + search.split(' ').join('|') + ")", "gi");
            var str = '<div class="autocomplete-suggestion" id="category" data-slug="' + item[4] + '" data-category="' + item[3] + '">' + '<font color="bbbbbb" style="font-style: italic">' + 'Категория, ' + '</font>'+ item[3].replace(re, "<b>$1</b>") + '</div>';
            $('#category').append('<div class="autocomplete-suggestion" data-detail="' + item[0] + '" data-make="' + item[1] + '" data-oem="' + item[2] + '">' + item[0].replace(re, "<b>$1</b>") + '</div>');
            return str;
        }

但仍然只显示类别。告诉我,问题是什么?

1 个答案:

答案 0 :(得分:0)

这个怎么样?

let arr = [
    {
        name: "hello world",
        brand: 3,
        oem: "apple",
        category: "zoo"

    },{
        name: "qwert",
        brand: 1,
        oem: "food",
        category: "atest"
    },{
        name: "alice",
        brand: 2,
        oem: "orange",
        category: "alicee"

    },
]

console.log(arr)

let sorted = arr.sort(function(a,b){

    if (a.category < b.category) {
        return -1
    }else if (a.category > b.category){
        return 1
    }
    return 0
})

console.log('--sorted--')
console.log(sorted)

结果:

[ { name: 'hello world', brand: 3, oem: 'apple', category: 'zoo' },
  { name: 'qwert', brand: 1, oem: 'food', category: 'atest' },
  { name: 'alice', brand: 2, oem: 'orange', category: 'alicee' } ]
  --sorted--
  [ { name: 'alice', brand: 2, oem: 'orange', category: 'alicee' },
  { name: 'qwert', brand: 1, oem: 'food', category: 'atest' },
  { name: 'hello world', brand: 3, oem: 'apple', category: 'zoo' } ]