如何在已经是内部JSON对象的JSON对象上应用排序

时间:2014-04-15 21:22:18

标签: javascript json

我有这个JSON对象:

 {
        "content":
        {
            "data":
            {
                "totalCost":244112,
                    "currency":"USD",
"summary": [
                {
                    "category": "NewCars",
                    "count": 2,
                    "cost": 91233,
                    "details": [
                        {
                            "name": "Ferrari",
                            "Model": "Enzo",
                            "condtion": "Excellent",
                            "kms": 10000,
                            "Year": 2009,
                            "cost": 69864
                        },
                        {
                            "name": "RollsRoyce",
                            "Model": "Ghost",
                            "condtion": "OK",
                            "kms": 10000,
                            "Year": 2006,
                            "cost": 21369
                        }
                    ]
                },
                {
                    "category": "UsedCars",
                    "count": 2,
                    "cost": 146464,
                    "details": [
                        {
                            "name": "Buggati",
                            "Model": "Veyron",
                            "condtion": "Excellent",
                            "kms": 10000,
                            "actionYear": 2011,
                            "cost": 85500
                        },
                        {
                            "name": "Lamborgini",
                            "Model": "Aventador",
                            "condtion": "OK",
                            "kms": 10000,
                            "Year": 2010,
                            "cost": 60964
                        }
                    ]
                },
                {
                    "category": "TwoWheelers",
                    "count": 1,
                    "cost": 6415,
                    "details": [
                        {
                            "name": "Suzuki",
                            "Model": "Hayabusa",
                            "condtion": "Bad",
                            "kms": 10000,
                            "Year":2009,
                            "cost": 6415
                        }
                    ]
                }
            ]
            }
        ,"metaData":
            {
                "title":"Details"
            }
        }
    ,"status":200
    }

有没有办法在内部键上对上面的JSON进行排序,比如Javascript中的名称,模型,条件,公里,年份和成本

提前致谢。任何帮助,将不胜感激。

P.S。 JSON和脚本的新手

2 个答案:

答案 0 :(得分:0)

您可以在阵列上调用排序函数(https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/sort)。如果它是JSON对象的内部数组,只需在该内部键上调用sort。 sort函数进行了就地排序,所以即使数组是内部的,它也会被排序。

对于您的示例JSON,假设数据变量包含JSON,以下是有效的排序调用。

data.content.data.summary.sort(function(a, b){
   //add comparator logic here to define ordering between 2 elements
});

data.content.data.summary[0].details.sort(function(a, b){
   //add comparator logic here to define ordering between 2 elements
});

答案 1 :(得分:0)

假设你有parse JSON已经喜欢

var obj = JSON.parse(jsonString);

然后你只需要迭代对象,这样你就可以在每个数组上调用sort method,比较它们的项目所需的属性:

var propertyname = "name"; // or "Year" or whatever

var summary = obj.content.data.summary;
for (var i=0; i<summary.length; i++)
    summary[i].details.sort(function(a, b) {
        a = a[propertyname]; b = b[propertyname];
        return +(a>b)||-(b>a);
    });