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.

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

Comments

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);

Comments

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

1 Comment

Avoid use for-in for array, see more here: stackoverflow.com/questions/500504/…. 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.
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);

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.