1

I have a data in database like below

vm.result = [
{
  "_id": ObjectId("57c036a75f7635e80c5d5bc8"),
  "sharedPersonId": ObjectId("570dec75cf30bf4c09679deb"),
  "notificationDetails": [
    {
      "userId": "570dfae0e79bd384255b6471",
      "userName": "Vinay",
      "isRed": true 
    } 
  ] 
},
{
  "_id": ObjectId("57bef418600b4350280f0b2f"),
  "sharedPersonId": ObjectId("570dec75cf30bf4c09679deb"),
  "notificationDetails": [
    {
      "userId": "56fe44836ce2226431f5388f",
      "userName": "Hemanth",
      "isRed": true 
    } 
  ] 
 },
 {
   "_id": ObjectId("57bef1a1d985a82c24e0c49b"),
   "sharedPersonId": ObjectId("570dec75cf30bf4c09679deb"),
   "notificationDetails": [
     {
       "userId": "57443657ee5b5ccc30c4e6f8",
       "userName": "Kevin",
       "isRed": true 
     } 
   ] 
  }                 
]

To access isRed, I have used for loop in angular.js file like below

for(var key in vm.result){
  for(var key1 in vm.result[key].notificationDetails){
     console.log(vm.result[key].notificationDetails[key1].isRed);
  }
}

The result shows true true true The isRed contains possibilities of true and false. I want to get the count how much number of true's are there. How to solve this please help me.

Emissary
  • 9,954
  • 8
  • 54
  • 65
Kevin
  • 653
  • 2
  • 13
  • 34

4 Answers4

2
var trueCount = 0;
var falseCount = 0;
for(var key in vm.result){
  for(var key1 in vm.result[key].notificationDetails){
     if(vm.result[key].notificationDetails[key1].isRed){
         trueCount ++ ;
     }else{
         falseCount ++;
     }
  }
}

This way you can keep track of true's and false's.

Rakesh Chand
  • 3,105
  • 1
  • 20
  • 41
2
var count = 0;
for(var key in vm.result){
  for(var key1 in vm.result[key].notificationDetails){
     if(vm.result[key].notificationDetails[key1].isRed)
         count++ ;

  }
}

console.log(count);
Sachila Ranawaka
  • 39,756
  • 7
  • 56
  • 80
2
function countResult(vm) {
  var total = 0;
  vm.result.forEach(function(item) {
    total += countNotifications(item.notificationDetails);
  });
  return total;
}

function countNotifications(notifications) {
  var count = 0;
  notifications.forEach(function(notification) {
    count += (notification.isRed ? 1 : 0)
  });
  return count;
}
ramon.liu
  • 186
  • 3
  • 5
  • Avoid use for-in for array, see more here: http://stackoverflow.com/questions/500504/why-is-using-for-in-with-array-iteration-a-bad-idea. Actually if you know it's an normal array, I don't think there is big difference. But just keep best practice to avoid error. – ramon.liu Aug 29 '16 at 07:40
2
var isRedCount = vm.result.reduce(
    (c, res) => c + res.notificationDetails.filter(d => d.isRed).length
, 0);

Array.prototype.reduce()

var vm = {};
vm.result = [{
  "_id": "57c036a75f7635e80c5d5bc8",
  "sharedPersonId": "570dec75cf30bf4c09679deb",
  "notificationDetails": [{
    "userId": "570dfae0e79bd384255b6471",
    "userName": "Vinay",
    "isRed": true
  }]
}, {
  "_id": "57bef418600b4350280f0b2f",
  "sharedPersonId": "570dec75cf30bf4c09679deb",
  "notificationDetails": [{
    "userId": "56fe44836ce2226431f5388f",
    "userName": "Hemanth",
    "isRed": true
  }]
}, {
  "_id": "57bef1a1d985a82c24e0c49b",
  "sharedPersonId": "570dec75cf30bf4c09679deb",
  "notificationDetails": [{
    "userId": "57443657ee5b5ccc30c4e6f8",
    "userName": "Kevin",
    "isRed": true
  }]
}];

var isRedCount = vm.result.reduce(
  (c, res) => c + res.notificationDetails.filter(d => d.isRed).length, 0);
console.log('isRedCount', isRedCount);
Emissary
  • 9,954
  • 8
  • 54
  • 65