2
$http
   .get('/getFollowings/' + currentUser)
   .success(function(response) {
        $scope.friendlist = response;
   });

I want to get the data which in response.but i cannot handle those values individually. 'response' contain :

[{"_id":"597c9fabc1ada32277f1da34","following":[{"username":"him"},{"username":"ron"},{"username":"nadu"}]}]

I want to get this usernames.

alexmac
  • 19,087
  • 7
  • 58
  • 69

4 Answers4

2

I recommend to use lodash library , try this :

$scope.friendlist = _.chain(response.plain())
                     .map(function(item){
                        item.following = _.pluck(item.following,'username')
                        return item;
                     })
                     .pluck('following')
                     .flatten()
                     .value();
Mumin Korcan
  • 101
  • 7
0

You can use angular.forEach

$scope.usernames = [];
$http.get('/getFollowings/' + currentUser)
    .success(function(response) {
      $scope.friendlist = response;
      angular.forEach($scope.friendlist[0].following, function(val) {
          $scope.usernames.push(val.username)
      });
    });

Here $scope.usernames is an array with all the values of usernames

You can use ng-repeat to display these values in the view.

 var myApp = angular.module('myApp', []);
 myApp.controller('ctrl', ['$scope', function($scope) {
     var response = [{
         "_id": "597c9fabc1ada32277f1da34",
         "following": [{
             "username": "him"
         }, {
             "username": "ron"
         }, {
             "username": "nadu"
         }]
     }];
     $scope.usernames = [];
     angular.forEach(response[0].following, function(val) {
         $scope.usernames.push(val.username)
     });
 }]);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" ng-controller="ctrl">
<div ng-repeat="username in usernames"><span>{{username}}</span></div>
</div>
Vivz
  • 6,625
  • 2
  • 17
  • 33
0

If you are using $http you need to serve Object to client.

Structure:

{
  "data": [
    {
      "_id": "597c9fabc1ada32277f1da34",
      "following": [
        {
          "username": "him"
        },
        {
          "username": "ron"
        },
        {
          "username": "nadu"
        }
      ]
    }
  ]
}

In your controller:

$scope.friendlist = response.data;
//i think it's simple
for(var i=0;i<$scope.friendlist.length;i++){
    console.log($scope.friendlist[i]);
}
hurricane
  • 6,521
  • 2
  • 34
  • 44
0

Just use forEach loop to get all username from the array.

    $scope.usernameList = [];
    $http
    .get('/getFollowings/' + currentUser)
    .success(function(response) {
       $scope.friendlist = response;           
       $scope.friendlist[0].following.forEach(function(item){
       $scope.usernameList.push(item.username);  
       });
   }); 

So now $scope.usernameList contains all the usernames which are in the array named following.

Surjeet Bhadauriya
  • 6,755
  • 3
  • 34
  • 52