6

Here i like to explain my problem clearly.

$http.get('arealist.json').success(function(response){
      angular.forEach(response, function(data){
           $scope.arealist = data.area_name;
           console.log($scope.arealist);
      });
});

Using the above code am getting area_name from arealist.json. and it looks like below image in console. console.log image

but i need the store above data in a array and it look like below

$scope.arealist = [ 
                    "Avadi", 
                    "Poonamallee",
                    "Padi", 
                    "Ambattur Industrial Estat",
                    "Annanagar",
                    "Arumbakkam",
                    "Kodambakkam",
                    "Nandanam"
                  ]

How can i do this ?

4 Answers 4

4

Declare a variable like below.

var arealistArray=[];
$scope.arealist=[]; 

Then push a value into array.

angular.forEach(response, function(data){
       arealistArray.push(data.area_name);
  });

Finally Assign the array to scope variable.

$scope.arealist=arealistArray;
Sign up to request clarification or add additional context in comments.

Comments

1

Use an array:

$scope.arealist = [];
$http.get('arealist.json').success(function(response){
      angular.forEach(response, function(data){
           $scope.arealist.push(data.area_name);
           console.log($scope.arealist);
      });
});

3 Comments

getting error as Cannot read property 'push' of undefined
Have you declared $scope.arealist = [] ? See the first statement in the code snippet.
I dont think this is possible. Can you recreate the issue in a plunker/fiddle?
1

It looks like you are performing this request within a controller (as $scope is available)? So first of all, you should be doing requests like this within a service. For example:

angular
    .module(/*module name*/)
    .service('AreaService', function ($http) {

        // Map the array of area objects to an array
        // of each area object's `area_name` property
        this.getAreas = function () {
            return $http
                .get('arealist.json')
                .then(function (response) {
                    return response.data.map(function (data) {
                        return data.area_name;
                    });
                });
        };
    })

And then consume the service within your controller:

    .controller(/*controller name*/, function (AreaService) {
        AreaService.getAreas().then(function (areas) {
            $scope.arealist = areas;
        }); 
    });

2 Comments

Approach is good, but mistakes in code. AreaService::getAreas should not use success handler but use then handler where the response data is transformed and returned. In the controller code, service API should be consumed as a then callback instead of passing the function as argument since that is never invoked.
@PrashantPalikhe Thanks! That'll teach me for trying to answer questions on a hangover. :-)
0

You can use Array.prototype.map() to achieve this.

$scope.arealist = [];

$http.get('arealist.json').success(function (response) {
    $scope.arealist = response.map(function (data) {
        return data.area_name;
    });
});

Browser support for map.

If you are using libraries like underscore.js or lodash, use _.map() for the same result.

$scope.arealist = [];

$http.get('arealist.json').success(function (response) {
    $scope.arealist = _.map(response, function (data) {
        return data.area_name;
    });
});

Comments

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.