2017-01-05 16 views
0

私はAPI応答データをlodashを使ってソートしようとしています。私のAPIは次のとおりです。https://www.clickpost.in/api/v2/track-order/?username=testuser&key=2e9b19ac-8e1f-41ac-a35b-4cd23f41ae17&waybill=3515341&cp_id=10角度を使用してAPIでデータをソートしますか?

APIデータは次のようである:

{ 
    "result": { 
     "3515341": { 
      "latest_status": { 
       "clickpost_status_code": 8, 
       "remark": "Delivered on 03 Nov 16 - Signed by sign", 
       "timestamp": "2016-11-03 08:10:56+00:00", 
       "location": "MUMBAI, MUMBAI", 
       "clickpost_status_description": "Delivered", 
       "status": "Delivered" 
      }, 
      "valid": true, 
      "scans": [ 
       { 
        "clickpost_status_code": 5, 
        "remark": "Arrived in", 
        "timestamp": "2016-10-27 22:19:18+00:00", 
        "location": "DELHI, DELHI", 
        "clickpost_status_description": "InTransit", 
        "status": "In Transit to" 
       }, 
       { 
        "clickpost_status_code": 5, 
        "remark": "In Transit to", 
        "timestamp": "2016-10-27 22:18:57+00:00", 
        "location": "DELHI, DELHI", 
        "clickpost_status_description": "InTransit", 
        "status": "In Transit to" 
       }, 
       { 
        "clickpost_status_code": 6, 
        "remark": "Out For Delivery", 
        "timestamp": "2016-10-31 13:29:24+00:00", 
        "location": "MUMBAI, MUMBAI", 
        "clickpost_status_description": "OutForDelivery", 
        "status": "Out For Delivery" 
       }, 
       { 
        "clickpost_status_code": 1, 
        "remark": "OrderPlaced", 
        "timestamp": "2016-10-26 16:33:23.650953+00:00", 
        "location": "None", 
        "clickpost_status_description": "OrderPlaced", 
        "status": "OrderPlaced" 
       }, 
       { 
        "clickpost_status_code": 8, 
        "remark": "Delivered on 03 Nov 16 - Signed by sign", 
        "timestamp": "2016-11-03 08:10:56+00:00", 
        "location": "MUMBAI, MUMBAI", 
        "clickpost_status_description": "Delivered", 
        "status": "Delivered" 
       }, 
       { 
        "clickpost_status_code": 5, 
        "remark": "Arrived at", 
        "timestamp": "2016-10-29 12:31:05+00:00", 
        "location": "MUMBAI, MUMBAI", 
        "clickpost_status_description": "InTransit", 
        "status": "Arrived at" 
       }, 
       { 
        "clickpost_status_code": 5, 
        "remark": "In Transit to", 
        "timestamp": "2016-10-28 00:15:32+00:00", 
        "location": "MUMBAI, MUMBAI", 
        "clickpost_status_description": "InTransit", 
        "status": "In Transit to" 
       }, 
       { 
        "clickpost_status_code": 4, 
        "remark": "Picked up and Booking processed", 
        "timestamp": "2016-10-27 22:15:13+00:00", 
        "location": "SOUTH DELHI, DELHI", 
        "clickpost_status_description": "PickedUp", 
        "status": "Picked up and Booking processed" 
       } 
      ] 
     } 
    }, 
    "meta": { 
     "success": true, 
     "message": "SUCCESS", 
     "status": 200 
    } 
} 

のjsファイルがある:それは私の追跡順序を示すことができたので、

var username="testuser"; 
    var key ="2e9b19ac-8e1f-41ac-a35b-4cd23f41ae17"; 
    var waybill ="3515341"; 
    var cp_id = "10"; 
$http({ 
    url: "https://www.clickpost.in/api/v2/track-order/", 
    params: { 
     username:username, 
     key:key, 
     waybill:waybill, 
     cp_id:cp_id 
    }, 
    method: 'GET' 
    }).then(function(response){ 
     $scope.track = response.data; 

     var sortData = response.data; 
     sortData = _.sortBy(sortData, ['clickpost_status_code']); 
     console.log(sortData); 

     $log.info(response); 
     // console.log(response.data); 
    }, function error(response) { 
     // $scope.error = response.data; 
     $scope.error = "wrong"; 
     $log.info(response); 
    }); 

は "clickpost_status_code" に基づいてデータをフェッチする必要があります状態。どうすればいいのか教えてください。 response.dataを想定し

+0

は介してアクセス配列です:' response.data.result ['3515341']。スキャンします。 JavaScriptオブジェクトのネストされたプロパティにアクセスする方法をGoogleに教えてください。 – Adam

答えて

0

はペイロードである:あなたが並べ替えるために必要なもの、response.data`はあなたが上記の投稿した応答であると仮定すると、 `

var sorted = response.data.result[3515341].scans.sort(function(a,b){ 
    return a.clickpost_status_code - b.clickpost_status_code; 
}) 
関連する問題