下划线,如何在密钥嵌套时进行排序

时间:2015-10-21 09:37:04

标签: json sorting nested underscore.js

我有一个复杂的JSON数组:典型的数组元素如下:

{
    "runner": {
        "_id": "5625ebf3d0ca02d7092d46e3",
        "title": "pid",
        "url": "runner",
        "user": {
            "_id": "5w24e437wb35c31w4fc47wf2",
            "avatar": {
                "src": "cew52ee16r037fc89625946621226597c7922ac.jpg"
            }
        },
        "start_date": "2015-11-11T19:30:00.000Z",
        "end_date": "2015-11-11T21:30:00.000Z",
        "privacy": {
            "scope": "public"
        },
        "tqx_payment": {
            "invoice_data": {
                "tax_name": "",
                "tax_rate": 0,
                "vat_number": "",
                "address": "",
                "city": "",
                "state": "",
                "country": "",
                "company": ""
            },
            "currency": "USD"
        },
        "published": true,
        "deleted": false,
        "timezone": "Europe/Rome"
    },
    "_id": "56264096a1ae9d316635b9b5",
    "type": "default"
}

我想通过 runner.start_date 对数组中的所有元素进行排序。这可能吗?怎么样?

1 个答案:

答案 0 :(得分:1)

[求助]通过这种方式的反复试验:

_.sortBy(JSONArray, function(itm) {
   return [itm.runner.start_date];
});