2014-01-24 140 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最後一個參數就足夠了。