0

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;
       }
    }
};
1
  • Well your if is always true, because you have to do == instead of = Commented Jan 28, 2016 at 20:51

4 Answers 4

4

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;
};
Sign up to request clarification or add additional context in comments.

2 Comments

=== if you want to avoid type coercion
I like to live dangerously.
1

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

Comments

1

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

Comments

0

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

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.