2014-01-24 141 views
-1

我有这个数组,我想知道是否可以将它与属性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" 
      } 
     ] 
    } 
}, 
+0

另外请注意,你的问题**没有**与JSON做。在解析JSON之后,你正在处理常规的JS对象和数组, –

回答

0

Underscore.js与数组和对象的工作提供了极大的实用功能。对于你的情况,你可以使用_.sortBy(...)查看http://underscorejs.org/#sortBy了解更多详情。对于简单的对象属性,指定对象属性名称als最后一个参数就足够了。