Skip to content

Instantly share code, notes, and snippets.

@voznik
Forked from Cacodaimon/SumByKey.js
Last active September 10, 2015 14:18
Show Gist options
  • Select an option

  • Save voznik/45d2ad832251fbeecd95 to your computer and use it in GitHub Desktop.

Select an option

Save voznik/45d2ad832251fbeecd95 to your computer and use it in GitHub Desktop.

Revisions

  1. @Cacodaimon Cacodaimon created this gist Nov 4, 2013.
    15 changes: 15 additions & 0 deletions SumByKey.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,15 @@
    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;
    };
    });
    2 changes: 2 additions & 0 deletions example.html
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,2 @@
    <!-- $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>