0

我可以从 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
   }
]

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

4

1 回答 1

2

您可以使用forEach

$scope.processedJson = [];
angular.forEach($scope.data.points, function (val) {
    $scope.processedJson.push({
        lat: val.lat,
        long: val.long
    });
});
于 2012-10-20T17:02:39.973 回答