After going through several discussions on this topic, I seem to be struggling with the concept. I have an array of objects with different properties and values. My goal is to count a specific property in the array only if its value is true.
In the JSON data provided below, I am required to iterate over each object in the array and tally only those where "IsPartyEnabled" is set to true. In this case, the count would be 3 as per the given JSON. The end result should return the value "3" 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 attempted a solution but hit a roadblock since I suspect the property may remain undefined resulting in no success.
$scope.partyEnabled = function () {
for (var i = 0; i < $scope.FunToBeHad.length; ++i) {
if($scope.FunToBeHad[i].IsPartyEnabled = true ) {
return i;
}
}
};