Skip to content

Instantly share code, notes, and snippets.

@Shaun818
Forked from Cacodaimon/SumByKey.js
Last active August 29, 2015 14:16
Show Gist options
  • Select an option

  • Save Shaun818/90f97d1d5ec368803688 to your computer and use it in GitHub Desktop.

Select an option

Save Shaun818/90f97d1d5ec368803688 to your computer and use it in GitHub Desktop.
<!-- $scope.myList = [{name: 'Foo', total: 1}, {name: 'Bar', total: 2}, {name: 'Baz', total: 3}] -->
<span class="badge badge-success pull-right">{{myList|sumByKey:'total'}}</span>
angular.module('caco.feed.filter', [])
.filter('sumByKey', function() {
return function(data, key) {
if (typeof(data) === 'undefined' || typeof(key) === 'undefined') {
return 0;
}
var sum = 0;
for (var i = data.length - 1; i >= 0; i--) {
sum += parseInt(data[i][key]);
}
return sum;
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment