1

I have a JSONfile like this:

vm.names = [{
  'name': 'A'
}, {
  'name': 'A'
}, {
  'name': 'B'
}, {
  'name': 'C'
}, {
  'name': 'C'
}]

I using AngularJS filter repeat it in HTML:

<ul ng-repeat ="name in vm.names | unique'name'>
<li>{{name.name}}</li>
</ul>

Here is issue

  • A
  • B
  • C

    So I want count item, I want display like this

  • A num:2
  • B num:1
  • C num:2

    How can I do that?

  • 3
    • You want to count them with what purpose? I mean, suppose you have a way to do it, what do you need the number for .... in your html, would you bind it to any element or something like that? Commented Apr 7, 2017 at 15:43
    • I want count item in store :) Commented Apr 7, 2017 at 16:01
    • 2
      thank you , it work perfect Commented Apr 7, 2017 at 16:06

    1 Answer 1

    3

    Just use the groupBy filter provided by angular-filter like in this runnable fiddle demo:

    View

    <div ng-controller="MyCtrl">
      <ul ng-repeat="item in names | groupBy: 'name' ">
          <li>{{item[0].name}}: {{ item.length}}</li>
      </ul>
    </div>
    

    AngularJS application

    var myApp = angular.module('myApp', ['angular.filter']);
    
    myApp.controller('MyCtrl', function($scope) {
      $scope.names = [{
        'name': 'A'
      }, {
        'name': 'A'
      }, {
        'name': 'B'
      }, {
        'name': 'C'
      }, {
        'name': 'C'
      }];
    });
    
    Sign up to request clarification or add additional context in comments.

    Comments

    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.