AngularJS Json重组

时间:2012-10-20 04:07:51

标签: angularjs

我可以从Angular函数中的某些服务中获取json,但是,有太多不必要的东西,所以我试图重构Json以仅包含我需要的数据。

$http({
            method: 'GET',
            url: 'from some service'}   

        }).success(function(data, status) {

           //this is Json data where I need to remove many things
           $scope.data = data;  

           //this is Json data I need
           $scope.processedJson = [];
      }

假设$ scope.data是

{
   "points":[
      {
         "address":"balh",
         "lat":123,
         "long":456
      },
      {
         "address":"balh",
         "lat":321,
         "long":543
      },
      {
         "address":"balh",
         "lat":432,
         "long":333
      }
   ]
}

$ scope.processedJson最终我需要的是

[
   {
      "lat":123,
      "long":456
   },
   {
      "lat":321,
      "long":543
   },
   {
      "lat":432,
      "long":333
   }
]

我如何运行一些循环来提取我需要的数据?

1 个答案:

答案 0 :(得分:2)

您可以使用forEach

$scope.processedJson = [];
angular.forEach($scope.data.points, function (val) {
    $scope.processedJson.push({
        lat: val.lat,
        long: val.long
    });
});