billy_comic
billy_comic

Reputation: 909

Count Items In An Array Whose Values = True

I've looked in many of the threads that discuss this, but maybe I'm just missing the concept. I have an array of objects that contain properties with values. I need to count a specific property in the array of objects only if it's value = true.

In the JSON below, I need to loop over every item in the array and then count only items where "IsPartyEnabled" evaluates to true. So, the count from the below JSON would = 3. I then need to return "3" back to my view.

FunToBeHad [{
    "IsFunAllowed": true,
    "IsPartyEnabled": true,
    "IsJoyRefillable": true,
},{
    "IsFunAllowed": true,
    "IsPartyEnabled": false,
    "IsJoyRefillable": true,
},{
    "IsFunAllowed": true,
    "IsPartyEnabled": true,
    "IsJoyRefillable": true,
},{
    "IsFunAllowed": true,
    "IsPartyEnabled": true,
    "IsJoyRefillable": true,
}]

I have tried this, but get stuck as I believe the property will still be undefined. Having no luck.

$scope.partyEnabled = function () {

    for (var i = 0; i < $scope.FunToBeHad.length; ++i) {
       if($scope.FunToBeHad[i].IsPartyEnabled = true ) {
           return i;
       }
    }
};

Upvotes: 0

Views: 109

Answers (4)

dexhering
dexhering

Reputation: 422

You can use the filter method, which makes this function is to return a new array with all elements that pass the test prescribed. And then you measure the number of items that passed the test and return it to their number "length" property

For more information: https://developer.mozilla.org/es/docs/Web/JavaScript/Referencia/Objetos_globales/Array/filter

$scope.partyEnabled = function () {
    return $scope.FunToBeHad.filter(function(elem){
        return elem.IsPartyEnabled;
    }).length;
};

Upvotes: 1

vlin
vlin

Reputation: 825

If you wanted to inject $filter into your controller, you could also do it like this:

$scope.partyEnabled = function() {
    return $filter('filter')($scope.FunToBeHad, {IsPartyEnabled: true}).length;
};

Upvotes: 1

serhiyb
serhiyb

Reputation: 4833

As suggested in other answers you are miss-using =/==/=== Except that I would suggest to refactor your code to make it more reusable:

$scope.calcNumber = function (propertyToCheck, expectedValue) {
    var result = 0;
    for (var i = 0; i < $scope.FunToBeHad.length; ++i) {
       if($scope.FunToBeHad[i][propertyToBeTrue] === expectedValue) {
           ++result;
       }
    }
    return result;
};

and you use that like:

var x = $scope.calcNumber('IsPartyEnabled', true);

Upvotes: 0

Mark Cidade
Mark Cidade

Reputation: 100057

You should be using == for comparison instead of assigning everything to true with =. And you should be keeping count instead of returning the first index:

$scope.partyEnabled = function () {

var count = 0;

for (var i = 0; i < $scope.FunToBeHad.length; ++i) {
   if($scope.FunToBeHad[i].IsPartyEnabled == true ) {
       count++;
   }
}

return count;
};

Upvotes: 4

Related Questions