Here is my json-

{
    topalbums: {
      album: [
        {
          name: "The Very Best of Cher",
          playcount: 1634402,
          mbid: "a7e2dad7-e733-4bee-9db1-b31e3183eaf5",
          url: "http://www.last.fm/music/Cher/The+Very+Best+of+Cher",
          artist: {
            name: "Cher",
            mbid: "bfcc6d75-a6a5-4bc6-8282-47aec8531818",
            url: "http://www.last.fm/music/Cher"
          }
        }
      ]
    }
}

how to get data, through looping, artist.name of every upcoming object using angular controller?

share|improve this question
1  
Yes, You need to iterate the object as album is an array of objects – Satpal Mar 25 '16 at 6:36
    
In controller , how iterate? – Aditya Gupta Mar 25 '16 at 6:41
    
$scope.data = topTracksService.list; $scope.tracks = $scope.data.track; console.log($scope.tracks); angular.forEach($scope.tracks, function(value, key) { }); Above code not working – Aditya Gupta Mar 25 '16 at 6:42

Let's say you have this json in $scope.allAlbums.

You can iterate the artists name of all the albums using

$scope.allAlbums.topalbums.album.forEach(function(item) {

    console.log(item.artist.name)
})
share|improve this answer
    
Got error- angular.min.js:111 TypeError: Cannot read property 'album' of undefined – Aditya Gupta Mar 25 '16 at 6:54
    
are you able to print $scope.allAlbums.topalbums in console? – Dhananjaya Kuppu Mar 25 '16 at 6:59
    
Your error says there is no topalbums in $scope.allAlbums. Check up to that level and make sure your $scope.allAlbums contains the given json. – Charlie H Mar 25 '16 at 7:05

You may try this. names will contain array album names:

var names = data.topalbums.album.map(function(item){
               return item.artist.name;
});
share|improve this answer

try this.in controller you can use angular foreach like this.

var app = angular.module("app",[])
app.controller('ctrl',['$scope', function($scope){
       $scope.data={
         topalbums: {
           album: [
           {
             name: "The Very Best of Cher",
             playcount: 1634402,
             mbid: "a7e2dad7-e733-4bee-9db1-b31e3183eaf5",
             url: "http://www.last.fm/music/Cher/The+Very+Best+of+Cher",
             artist: {
                name: "Cher",
                mbid: "bfcc6d75-a6a5-4bc6-8282-47aec8531818",
                url: "http://www.last.fm/music/Cher"
              }
             },
            {
             name: "The Very Best of Cher",
             playcount: 1634402,
             mbid: "a7e2dad7-e733-4bee-9db1-b31e3183eaf5",
             url: "http://www.last.fm/music/Cher/The+Very+Best+of+Cher",
             artist: {
                name: "Cher2",
                mbid: "bfcc6d75-a6a5-4bc6-8282-47aec8531818",
                url: "http://www.last.fm/music/Cher"
              }
             }
          ]
         }
       };
  
  
  angular.forEach($scope.data.topalbums,function(album){
    angular.forEach(album,function(a){
      alert(a.artist.name);
       console.log(a.artist.name);
      })
    
    })
}]);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.22/angular.min.js"></script>
<div ng-app="app" ng-controller="ctrl">
<div class="item item-checkbox">
   <div ng-repeat="album in data.topalbums">
         <p ng-repeat="a in album">{{a.artist.name}}</p>
</div>    
</div>

share|improve this answer
    
I want artist.name in controller part , not on view. In my problem, artist.name is passed to a function in my controller. – Aditya Gupta Mar 25 '16 at 6:48
    
i update my answer. – Hadi Mar 25 '16 at 6:53
    
It's not working. Nothing for display – Aditya Gupta Mar 25 '16 at 6:59
    
it's working. please see console. – Hadi Mar 25 '16 at 7:01

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.