I am using Angular for the first time, and i think i may have misunderstood how easy it is using this framework to calculate things like occurrence of nodes and node values.
I would like to be able to count for example the number of times the pant size for a player is 42.
Is this something that is easily possible with angular, in other words is there some quick call in the framework which counts occurrences of node values, or does it require a custom function with collections and key/value pairs?
function MyCtrl($scope) {
$scope.players = [
{shirt: 'XXL', pants: '42', shoes: '12'},
{shirt: 'XL', pants: '38', shoes: '10'},
{shirt: 'M', pants: '32', shoes: '9'},
{shirt: 'XXL', pants: '42', shoes: '12'},
{shirt: 'XXL', pants: '42', shoes: '12'},
{shirt: 'XXL', pants: '42', shoes: '12'}
];
}
In this fiddle i have utilized the help of another SO discussion to aggregate distinct node values. Now i would like help on the best practices for occurrence counts.
I would recommend that you use a filter in angular to limit your array based on whatever specification you need. You can inject the angular filter into your controller using the name "filterFilter", and then use it anywhere within that controller. For instance, if you wanted to determine the number of players with a pant size of 42 you could do something like:
var pantSizeOccurences = filterFilter($scope.players, { pants: '42' });
alert(pantSizeOccurences.length);
I updated your jsfiddle link. You would need to inject the filterFilter into your controller. This is a snippet to help you out.
var myApp = angular.module('myApp',[]);
function MyCtrl($scope, filterFilter) {
$scope.players = [
{shirt: 'XXL', pants: '42', shoes: '12'},
{shirt: 'XL', pants: '38', shoes: '10'},
{shirt: 'M', pants: '32', shoes: '9'},
{shirt: 'XXL', pants: '42', shoes: '12'},
{shirt: 'XXL', pants: '42', shoes: '12'},
{shirt: 'XXL', pants: '42', shoes: '12'}
];
var sizeFourtyTwoPants = filterFilter($scope.players, {pants: '42'});
alert(sizeFourtyTwoPants.length);
}
MyCtrl.$inject = ['$scope','filterFilter'];