0

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?

7
  • you can try using underscorejs(_) 's pluck method. Commented Dec 29, 2016 at 10:40
  • @VikashVerma and how do you know that he is having underscore? Commented Dec 29, 2016 at 10:41
  • 1
    @tanmay : I m suggesting him to use underscore inside angular controller. Commented Dec 29, 2016 at 10:43
  • 2
    $scope.episodes = theNumbers.map(function(x){ return x.episodeNumber}); Commented Dec 29, 2016 at 10:44
  • @VikashVerma this is fairly simple use-case and can be achieved without using underscore as well Commented Dec 29, 2016 at 10:44

2 Answers 2

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

return $scope.episodes.the_numbers.map(function(item){
    return item.episodeNumber;
});
Sign up to request clarification or add additional context in comments.

Comments

1

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' }}

2 Comments

Nice method, but it looks more complicated then the map function provided by Pleshi Bloy :)
Thanks! You indicated you wanted a filter. I assumed you wanted an angular filter. In any case, this one is reusable.

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.