0

I can get a json from some services in Angular function, however, there are too many unnecessary things so I am trying to restructure Json to include only data I need.

$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 = [];
      }

Let's say $scope.data is

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

And $scope.processedJson which is ultimately what I need to have is

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

How do I need to run some looping to extract the data I need?

1 Answer 1

2

You can use a forEach

$scope.processedJson = [];
angular.forEach($scope.data.points, function (val) {
    $scope.processedJson.push({
        lat: val.lat,
        long: val.long
    });
});
Sign up to request clarification or add additional context in comments.

1 Comment

So simple and easy.. I was stupid thinking about some complex manipulations.. Thanks :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.