简体   繁体   English

使用jQuery对JSON对象进行排序

[英]Ordering JSON Objects using jQuery

I have the following JSON Object being loaded into my application and stored into a var called obj: 我将以下JSON对象加载到我的应用程序中并存储到名为obj的var中:

{
    "items" : [
    {
        "name" : "item-1",
        "group" : [
        {
            "groupName" : "name-1",
            "groupPosition" : 2
        },
        {
            "groupName" : "name-2",
            "groupPosition" : 1
        }]
    },
    {
        "name" : "item-2",
        "group" : [
        {
            "groupName" : "name-1",
            "groupPosition" : 1
        },
        {
            "groupName" : "name-2",
            "groupPosition" : 2
        }]
    }]
}

I then do the following to go through it: 然后,我执行以下操作来完成它:

var groups = new Array();
var items = new Array();
$.each(obj.items, function(i,r){
    var itemName = r.name;
    $.each(r.group, function(index, record){
        if ($.inArray(record.groupName) == -1) {
            groups.push(record.groupName);
            $('body').append('<div id="record.groupName"></div>');
        }
        $('#'+record.groupName).append('<div id="itemName">itemName</div>');
        // At this point I'm stuck as the items get added in order of iteration,
        // not according to their record.groupPosition value.
    });
});

There will eventually be several hundred "items" each contained within an unset number of "groups". 最终将有数百个“项目”包含在未设置数量的“组”中。

The trouble I'm having is how to iterate through the JSON object using jQuery or good ol'JavaScript and display the items in the correct position within each group as the items and groups won't be listed inside the JSON object in sequential order. 我遇到的麻烦是如何使用jQuery或好的ol'JavaScript迭代JSON对象,并在每个组中的正确位置显示项目,因为项目和组不会按顺序列在JSON对象中。

Any help would be greatly appreciated! 任何帮助将不胜感激!

Thank you. 谢谢。

Why not just give the group items the position index like this: 为什么不直接给组项目位置索引:

{
"items" : [
{
    "name" : "item-1",
    "group" : {
    2:{
        "groupName" : "name-1",
        "groupPosition" : 2
    },
    1:{
        "groupName" : "name-2",
        "groupPosition" : 1
    }}

},
{
    "name" : "item-2",
    "group" : {
    1:{
        "groupName" : "name-1",
        "groupPosition" : 1
    },
    2:{
        "groupName" : "name-2",
        "groupPosition" : 2
    }}
}]

} }

Assuming you have a variable which is assigned to this: 假设您有一个分配给它的变量:

var data = ...

you could use the $.each() method: 你可以使用$.each()方法:

$.each(data.items, function(index, item) {
    // here item.name will contain the name
    // and if you wanted to fetch the groups you could loop through them as well:
    $.each(item.group, function(i, group) {
        // here you can use group.groupName and group.groupPosition
    });
});

Arrays ( [] ) in javascript are 0 index based and preserve their order when you are iterating over them. javascript中的数组( [] )基于0索引,并在迭代它们时保留它们的顺序。

If I understood correctly your problem it is not about the sorting it self but how to link them to your dom nodes, solution: use classes with numbers. 如果我正确理解你的问题,那不是关于自己的排序,而是如何将它们链接到你的dom节点,解决方案:使用带数字的类。

For example: 例如:

$(".group"+items[1].group[0].grouposition").append(items[1].group[0].name);
// this will give append to the element with class="group1"

If you join this with having the html structure that is being generated to match the same names, then it won't be a problem and you don't have to sort them 如果你加入这个与生成的html结构匹配相同的名称,那么它不会是一个问题,你不必排序它们

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM