How do I iterate through the properties of a JavaScript object and count the occurrences of a specific value?
angular.module('App',[])
.controller('mainCtrl', ['$scope', function ($scope) {
var data = {
"plans":[
{
"id":1,
"code":"NEXT_DAY_500gm",
"name":"Next Day less than 500gm",
"cost":55,
"duration":24,
"min_weight":0,
"max_weight":500,
"COD":1,
"created_at":"-0001-11-30 00:00:00",
"updated_at":"-0001-11-30 00:00:00"
},
{
"id":2,
"code":"NEXT_DAY_500gm_1kg",
"name":"Next Day 500gm to 1kg",
"cost":70,
"duration":24,
"min_weight":501,
"max_weight":1000,
"COD":1,
"created_at":"-0001-11-30 00:00:00",
"updated_at":"-0001-11-30 00:00:00"
},
{
"id":3,
"code":"NEXT_DAY_1kg_2kg",
"name":"Next Day 1kg to 2kg",
"cost":95,
"duration":24,
"min_weight":1001,
"max_weight":2000,
"COD":1,
"created_at":"-0001-11-30 00:00:00",
"updated_at":"-0001-11-30 00:00:00"
},
{
"id":4,
"code":"SAME_DAY_500gm",
"name":"Same Day less than 500gm",
"cost":95,
"duration":8,
"min_weight":0,
"max_weight":500,
"COD":1,
"created_at":"-0001-11-30 00:00:00",
"updated_at":"-0001-11-30 00:00:00"
},
{
"id":5,
"code":"SAME_DAY_500gm_1kg",
"name":"Same Day 500gm to 1kg",
"cost":130,
"duration":8,
"min_weight":501,
"max_weight":1000,
"COD":1,
"created_at":"-0001-11-30 00:00:00",
"updated_at":"-0001-11-30 00:00:00"
},
{
"id":6,
"code":"SAME_DAY_1kg_2kg",
"name":"Same Day 1kg to 2kg",
"cost":165,
"duration":8,
"min_weight":1001,
"max_weight":2000,
"COD":1,
"created_at":"-0001-11-30 00:00:00",
"updated_at":"-0001-11-30 00:00:00"
}
]
};
$scope.data1=data;
}]);
I am looking to tally the number of times the "status" field is marked as "COMPLETE" in order to assign that count to a variable. For instance, if "COMPLETE" appears 4 times, my sale variable will be set to 4. Any suggestions on how to achieve this?