rossanmol
rossanmol

Reputation: 1653

AngularJS array separate by comma

I have the following JSON which will be set to $scope.episodes;

"the_numbers": [{
    "episodeID": 16503,
    "episodeNumber": 183
},
{
    "episodeID": 16504,
    "episodeNumber": 184
}]

From this JSON I want to create a filter in angularjs to get the following output from $scope.episodes: 183, 184.

What has to be done to achieve that?

Upvotes: 0

Views: 1077

Answers (2)

Peshi Bloy
Peshi Bloy

Reputation: 66

$scope.episodes = {
    "the_numbers": [{
        "episodeID": 16503,
        "episodeNumber": 183
    },
    {
        "episodeID": 16504,
        "episodeNumber": 184
    }]
};

return $scope.episodes.the_numbers.map(function(item){
    return item.episodeNumber;
});

Upvotes: 2

Michael Sacket
Michael Sacket

Reputation: 907

Here is an option:

var app = angular.module('myApp', []);
app.filter('plucker', function() {
  return function(input, key) {
    input = input || [];
    var values = [];
    for (var i = 0; i < input.length; i++) {
      values.push(input[i][key]);
    }
    return values.join(', ');
  };
});

Then in your template you can use:

{{ episodes | plucker:'episodeNumber' }}

Upvotes: 1

Related Questions