javascript - iterate the json array and push each value to the list in angular js -
i new angular js.my question is, have json array getting ajax response php page. iterating json array , want push each value in list like
angular.foreach($scope.companies.area, function(value, key) { $scope.comp = [ { 'name': value1 }, { 'name': value2 }, { 'name': value3 } //... ] });
how can make list? json data is
{"1":"audugodi","2":"airforce station yelahanka","3":"agaram","4":"anadanagar","5":"arabic college","6" :"banasawadi","7":"banashankari","8":"banashankari ii stage","9":"banashankari iii stage","10":"bangalore city","11":"bangalore gpo","12":"bannerghatta","13":"bannerghatta road","14":"basavanagudi","15":"basaveswaranagar" }
it can easier using simple array.prototype.map
:
$scope.comp = $scope.companies.area.map(function(value) { return { name: value }; });
as data in object format, have change bit use .map (the original json data @ bottom reference):
helloapp.controller("companyctrl", function ($scope, $http) { $http.post('class_locaality.php?flag=1').success(function (data) { $scope.companies = data; // original data - need data.area $scope.comp = object.keys(data.area).map(function (key) { // turn object array of keys can iterate return { name : data.area[key] // value original data.area using key }; }); }).error(function (data) { // log error }); }); { "1" : "audugodi", "2" : "airforce station yelahanka", "3" : "agaram", "4" : "anadanagar", "5" : "arabic college", "6" : "banasawadi", "7" : "banashankari", "8" : "banashankari ii stage", "9" : "banashankari iii stage", "10" : "bangalore city", "80" : "st.thomas town", "81" : "subramanyanagar", "95" : "yelahanka", "96" : "yeshwanthpur" }
Comments
Post a Comment