1

How to get question open eyes 0 values length in angularjs?

My Plunker

  • In my plunker I got the solution for (i) total no of questions (ii) total no of upvotes and (iii) Open eyes total sum of [value 1], what we expecting is (iv) how to get Open Eyes 0 values length.

  • How to get Open Eyes [0 values] length, please look at my plunker for reference I don't know where I did the mistake

  • If I used this {{question.openeyes.length == '0'} the answer I got False, Expecting answer would be : 2

My Html :-

<p>Total no of questions :{{question.length}} </p>

<p>Total no of upvotes : {{resultValue | sumOfValue:'upvotes'}}</p>

<p>Total no of Open Eyes of [1 values] : {{resultValue | sumOfValue:'openeyes'}}</p>

<p class="color">Total no of Open Eyes of [0 value] : {{question.openeyes.length == '0'}}</p>

My Data :-

    $scope.question = [
   {
       "_id": "5936a70095e3a85804aae050",
       "user": {
           "_id": "58072aba0f82a61823c434df",
           "displayName": "Table 1",
           "roles": ["admin"],
           "profileImageURL": "./modules/users/client/img/ownprofile/uploads/1f08308f43b0674d61a2cc5d95deb5ef",
           "email": "ms@e21designs.com",
           "categories": []
       },
       "__v": 1,
       "openeyers": ["sarawana@gmail.com"],
       "openeyes": 1,
       "upvoters": ["sarawana@gmail.com"],
       "upvotes": 1,
       "title": "what is cricket",
       "created": "2017-06-06T12:58:40.204Z"
},

   {
       "_id": "5936a70095e3a85804aae050",
       "user": {
           "_id": "58072aba0f82a61823c434df",
           "displayName": "Table 1",
           "roles": ["admin"],
           "profileImageURL": "./modules/users/client/img/ownprofile/uploads/1f08308f43b0674d61a2cc5d95deb5ef",
           "email": "ms@e21designs.com",
           "categories": []
       },
       "__v": 1,
       "openeyers": [],
       "openeyes": 0,
       "upvoters": ["sarawana@gmail.com"],
       "upvotes": 1,
       "title": "who fan you are",
       "created": "2017-06-06T12:58:40.204Z"
},

   {
       "_id": "5936a70095e3a85804aae050",
       "user": {
           "_id": "58072aba0f82a61823c434df",
           "displayName": "Table 1",
           "roles": ["admin"],
           "profileImageURL": "./modules/users/client/img/ownprofile/uploads/1f08308f43b0674d61a2cc5d95deb5ef",
           "email": "ms@e21designs.com",
           "categories": []
       },
       "__v": 1,
       "openeyers": [],
       "openeyes": 0,
       "upvoters": [  "ms@e21designs.com", "vp@gmail.com"],
       "upvotes": 2,
       "title": "best of the day",
       "created": "2017-06-06T12:58:40.204Z"
},
{
       "_id": "5936a70095e3a85804aae050",
       "user": {
           "_id": "58072aba0f82a61823c434df",
           "displayName": "Table 1",
           "roles": ["admin"],
           "profileImageURL": "./modules/users/client/img/ownprofile/uploads/1f08308f43b0674d61a2cc5d95deb5ef",
           "email": "ms@e21designs.com",
           "categories": []
       },
       "__v": 1,
       "openeyers": ["ms@e21designs.com"],
       "openeyes": 1,
       "upvoters": ["ms@e21designs.com","vp@gmail.com", "ms@gmail.com"],
       "upvotes": 0,
       "title": "he is best",
       "created": "2017-06-06T12:58:40.204Z"
}]
  • If any one knows of the solution please update my plunker as well to know the exact solution thanks
Vivz
  • 6,625
  • 2
  • 17
  • 33
R. Mani Selvam
  • 320
  • 8
  • 36

1 Answers1

1

You can either create a filter or call a function to get the length of Open Eyes [0 values] length. But using a filter is more preferred because the latter has performance issue.

Method 1: Calling a function directly

JS:

  $scope.openZero=function(q){
    var count =0;
    for(var i=0;i<q.length;i++){
      if(q[i].openeyes==0)
       count ++;         
    }
    return count;
  }

HTML:

<p class="color">Total no of Open Eyes of [0 value] : {{openZero(question)}}</p>

Working Plunker: http://plnkr.co/edit/7XedtmD6JlqpkgM3B7on?p=preview


Method 2 : Using Filter

JS

.filter('sumOfZero', function() {
            return function(data, key) {
                if (angular.isUndefined(data) && angular.isUndefined(key)) return 0;
                var count = 0;
                for (var i = 0; i < data.length; i++) {
                    if (data[i].openeyes == 0) count++;
                }
                return count;
            }

HTML:

<p class="color">Total no of Open Eyes of [0 value] : {{resultValue | sumOfZeros:'openeyes'}}</p>

Working Plunker: http://plnkr.co/edit/RjNWeqWEYuzbBdew8s7V?p=preview

Vivz
  • 6,625
  • 2
  • 17
  • 33
  • Hi Vivz thanks for your valuable answer will check and update you asap... – R. Mani Selvam Aug 22 '17 at 07:51
  • Is there any possibility to count no of questions using instead of this `{{question.length}}` because it's not working in my project, it's working fine in plunker only, please update if you know the solution.... – R. Mani Selvam Aug 22 '17 at 08:15
  • Why did you remove the answer? – Vivz Aug 31 '17 at 08:59
  • by mistake, it happens cool and one more help if possible answer this question too: - https://stackoverflow.com/questions/45976603/how-to-filter-ng-repeat-value-on-clicking-of-checkbox-field-in-angularjs/45977433#45977433 thanks.. – R. Mani Selvam Aug 31 '17 at 09:01