Javascript 如何对数组进行排序或正确显示?

Javascript 如何对数组进行排序或正确显示?,javascript,jquery,Javascript,Jquery,使用此json并获取以下值: [[名称、品牌、oem、类别],[名称、品牌、oem、类别],[名称、品牌、oem、类别],[名称、品牌、oem、类别]] 我的js: $(function(){ $('input[name="oem"]').autoComplete({ minChars: 4, // parse json and output the values 11 source: function(term, response) { term = t

使用此json并获取以下值:

[[名称、品牌、oem、类别],[名称、品牌、oem、类别],[名称、品牌、oem、类别],[名称、品牌、oem、类别]]

我的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
       },
    });
 });
您需要获得:

Category
Category
Category
Name
Name
Name
Name
Name
试图通过
排序
执行此操作。但我认为需要使用数组,而不是数组。可以使用
附加
执行此操作

有一个自动完成的搜索。用这个

UPD:

使用append实现

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;
        }
renderItem:函数(项,搜索){
search=search.replace(/[-\/\^$*+.()|[\]{}]/g,\\$&');
var re=new RegExp(“(“+search.split(“”).join(“|”)+”,“gi”);
var str='+'+''''+'аааааааааааааа1072;
$('#category')。追加(''+项[0]。替换(re,“$1”)+”);
返回str;
}
但仍然只显示类别。告诉我,有什么问题吗?

这个怎么样

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)

更新的问题这是数组中的一个数组,你必须将每个子对象添加到一个新数组中,然后相应地显示。谢谢,但我更新了问题。我使用
append
。告诉我,有什么问题吗?
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' } ]