UNPKG

angular-filter

Version:

Bunch of useful filters for angularJS(with no external dependencies!)

38 lines (28 loc) 755 B
/** * @ngdoc filter * @name countBy * @kind function * * @description * Sorts a list into groups and returns a count for the number of objects in each group. */ angular.module('a8m.count-by', []) .filter('countBy', [ '$parse', function ( $parse ) { return function (collection, property) { var result = {}, get = $parse(property), prop; collection = (isObject(collection)) ? toArray(collection) : collection; if(!isArray(collection) || isUndefined(property)) { return collection; } collection.forEach( function( elm ) { prop = get(elm); if(!result[prop]) { result[prop] = 0; } result[prop]++; }); return result; } }]);