2

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.

http://jsfiddle.net/7kasV/1/

Cœur
  • 37,241
  • 25
  • 195
  • 267
Tony H.
  • 1,975
  • 4
  • 14
  • 20
  • I suggest you to use [underscorejs](http://underscorejs.org/) for filtering, searching etc or [Lo-Dash](http://lodash.com/). Also read [Differences between lodash and underscore](http://stackoverflow.com/questions/13789618/differences-between-lodash-and-underscore) – Satpal Jan 06 '14 at 17:55

1 Answers1

3

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'];
MDiesel
  • 2,647
  • 12
  • 14
  • Take a look at the following link for more information: http://docs.angularjs.org/guide/filter – MDiesel Jan 06 '14 at 17:58
  • i've updated the fiddle to try and put a filter in practice but i keep getting the error object is not a function. Any ideas what this error means. I sense that the error may either be talking about the 'filter' function or the collection, but i'm not sure. If you get a chance please take a look at the fiddle and help me identify what i might be missing. Thank you very much for any help/advice. http://jsfiddle.net/7kasV/5/ – Tony H. Jan 07 '14 at 19:35
  • Is there anyway to dynamically filter for the pant size while iterating through the pants collection in the html page. I'm trying to create a custom filter which can be called from the web page, which would mean it would have to be outside of the scope. This scope is what i'm having the most difficulty with, passing it in dynamically to a custom filter. Your help is helping tremendously and i appreciate the time you've spent to help answer my questions. – Tony H. Jan 07 '14 at 21:40
  • Antonio. Okay, I see what you mean, what do you want this custom filter to do, or what parameters should it take in? It looks like your filter currently returns the pant size for all unique items in the list – MDiesel Jan 07 '14 at 22:02
  • I'd like it to count the number of times a pant size occurs in the array. So for each iteration of the pant size unique item collection, the custom filter would be called to count the number of occurrences of that pant size. – Tony H. Jan 07 '14 at 22:59
  • I have this updated fiddle that is essentially giving me what i want, however the count is wrapped in a repeater. Is there anyway to have this functionality outside of a repeater? If so that would be my solution :) http://jsfiddle.net/7kasV/12/ – Tony H. Jan 08 '14 at 16:28
  • 1
    I updated your jsfiddle. It now does not have the inner repeater. You were very close. Nice job. Is that what you are looking for? – MDiesel Jan 08 '14 at 20:44
  • That is absolutely what i was looking for! Thank you very much for your help. I'm marking your answer as the solution. One quick question: Would you say this is an efficient approach, or inefficient and why? – Tony H. Jan 08 '14 at 21:40