【问题标题】:Sort nested array by properties [duplicate]按属性对嵌套数组进行排序[重复]
【发布时间】:2014-01-24 13:39:08
【问题描述】:

我有这个数组,我想知道是否可以分别使用属性comments.countlikes.count 对其进行排序。

例如,我可以使用likes.count 参数或comments.count 参数调用一个函数。

{
    "attribution": null,
    "tags": [

    ],
    "type": "",
    "location": null,
    "comments": {
        "count": 2,
        "data": [
            {
                "created_time": "1385670850",
                "text": "Holy shit",
                "from": {
                    "username": "someone",
                    "profile_picture": "http://url.com/profiles/profile_191120775_75sq_1365830292.jpg",
                    "id": "191120775"
                },
                "id": "599372997379438683"
            },
            {
                "created_time": "1385680581",
                "text": "",
                "from": {
                    "username": "someone",
                    "profile_picture": "http://url.com/profiles/profile_1523167_75sq_1389544912.jpg",
                    "id": "1523167"
                },
                "id": "599454624038205610"
            }
        ]
    },
    "likes": {
        "count": 6,
        "data": [
            {
                "username": "someone",
                "profile_picture": "http://url.com/profiles/profile_2169761_75sq_1389075971.jpg",
                "id": "2169761"
            },
            {
                "username": "rashmityagi",
                "profile_picture": "http://url.com/profiles/profile_393770264_75sq_1388911033.jpg",
                "id": "393770264"
            },
            {
                "username": "tylerferweda",
                "profile_picture": "http://url.com/profiles/profile_191120775_75sq_1365830292.jpg",
                "id": "191120775"
            },
            {
                "username": "cbolts",
                "profile_picture": "http://url.com/profiles/profile_1523167_75sq_1389544912.jpg",
                "id": "1523167"
            }
        ]
    }
},
{
    "attribution": null,
    "tags": [

    ],
    "type": "",
    "location": null,
    "comments": {
        "count": 10,
        "data": [
            {
                "created_time": "1385670850",
                "text": "Holy shit",
                "from": {
                    "username": "someone",
                    "profile_picture": "http://url.com/profiles/profile_191120775_75sq_1365830292.jpg",
                    "id": "191120775"
                },
                "id": "599372997379438683"
            },
            {
                "created_time": "1385680581",
                "text": "",
                "from": {
                    "username": "someone",
                    "profile_picture": "http://url.com/profiles/profile_1523167_75sq_1389544912.jpg",
                    "id": "1523167"
                },
                "id": "599454624038205610"
            }
        ]
    },
    "likes": {
        "count": 20,
        "data": [
            {
                "username": "someone",
                "profile_picture": "http://url.com/profiles/profile_2169761_75sq_1389075971.jpg",
                "id": "2169761"
            },
            {
                "username": "rashmityagi",
                "profile_picture": "http://url.com/profiles/profile_393770264_75sq_1388911033.jpg",
                "id": "393770264"
            },
            {
                "username": "tylerferweda",
                "profile_picture": "http://url.com/profiles/profile_191120775_75sq_1365830292.jpg",
                "id": "191120775"
            },
            {
                "username": "cbolts",
                "profile_picture": "http://url.com/profiles/profile_1523167_75sq_1389544912.jpg",
                "id": "1523167"
            }
        ]
    }
},

【问题讨论】:

  • 另请注意,您的问题与 JSON没有任何关系。解析完 JSON 之后,就是在处理常规的 JS 对象和数组,

标签: javascript sorting express


【解决方案1】:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort

json_array.sort(function(a, b) {
    if(a.comments.count < b.comments.count) {
        return -1;
    }
    if(a.comments.count > b.comments.count) {
        return 1;
    }
    return 0;
});

也可以修改为likes.count

【讨论】:

    【解决方案2】:

    Underscore.js 为处理数组和对象提供了出色的实用功能。对于您的情况,您可以使用 _.sortBy(...) 有关更多详细信息,请参阅http://underscorejs.org/#sortBy。对于简单的对象属性,指定对象属性名称作为最后一个参数就足够了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-06
      • 2023-01-06
      相关资源
      最近更新 更多