Skip to content


Subversion checkout URL

You can clone with
Download ZIP
Provides LINQ-like aggregate functions to jQuery.
JavaScript CSS
branch: master

jQuery Aggregate

Provides LINQ-like aggregate functionality to jQuery.

While jQuery has many useful array utility functions ($.map and $.grep for example), there's no built-in functionality that aggregates values. This is a small plugin built on top of jQuery that provides that functionality.

Getting Started

Download the production version or the development version.

In your web page:

        <script src="jquery.js"></script>
        <script src="dist/jQuery-Aggregate.min.js"></script>
            jQuery(function($) {
              // $.aggregate and $.sum are now available.

(Coming soon)


jQuery.aggregate(array [,seed], callback(workingValue, elementOfArray) [,resultFunction(finalValue)])

Type: Array
The array to aggregate.

seed (default value: null)
Type: Object
An initial "seed" value.

callback(workingValue, elementOfArray)
Type: Function()
The aggregating function called to incorporate the current element of the array into the aggregate value. The function should return the new working aggregate value.

resultFunction(finalValue) (default value: null)
Type: Function()
A function that's executed after the array has been aggregated into a single value. You can use this function to manipulate the final result

This is the "static" version of the function. You can also call .aggregate directly on a jQuery object.

jQuery.sum(array [,callback(elementOfArray)])

Type: Array
The array to sum.

Type: Function()
An optional function that recieves the element of the array being iterated over and returns a value to incorporate into the sum.



Simple use case:

var letters = "abcdefghijklmnopqrstuvwxyz"
    , message = $.aggregate([0, 6, 6, 17, 4, 6, 0, 19, 4], function (working, element) {
        return working ? working + letters.charAt(element) : letters.charAt(element);

    // message === "aggregate"


Supplying an initial seed value:

var letters = "abcdefghijklmnopqrstuvwxyz"
    , message = $.aggregate([0, 6, 6, 17, 4, 6, 0, 19, 4], '',  function (working, element) {
        return working + letters.charAt(element);

    // message === "aggregate"


Using a final "transformation function:"

var letters = "abcdefghijklmnopqrstuvwxyz"
    , message = $.aggregate([0, 6, 6, 17, 4, 6, 0, 19, 4], '',  function (working, element) {
        return working + letters.charAt(element);
    }, function (value) {
        return value.toUpperCase();

    // message === "AGGREGATE"


Using $.fn.aggregate on a jQuery object:

<form id="groceries">
                <td><label for="bread">Bread</label></td>
                <td><span class="price">2.50</span></td>
                <td><input id="bread" type="text" /></td>
                <td><label for="bologna">Bologna</label></td>
                <td><span class="price">4.00</span></td>
                <td><input id="bologna" type="text" /></td>
                <td><label for="cheese">Cheese</label></td>
                <td><span class="price">3.50</span></td>
                <td><input id="cheese" type="text" /></td>
                <td><label for="chips">Chips</label></td>
                <td><span class="price">3.00</span></td>
                <td><input id="chips" type="text" /></td>
    <p>Total: <span id="total"></span></p>
$(document).ready(function () {
    $("td input:text").on("change", function () {
        var total = $("td input:text").sum(function () {
            var quantity = this.value
                , cost = parseFloat($(this).closest("tr").find(".price").text(), 10) || 0;

            return quantity * cost;




Simple use case:

var total = $.sum([0, 6, 6, 17, 4, 6, 0, 19, 4]);


// total === 62


Using a transformation function:

var groceries = [
    { name: 'bread', price: 2.50 },
    { name: 'bologna', price: 4.00 },
    { name: 'cheddar cheese', price: 3.50 },
    { name: 'potato chips', price: 3.00 }
], total = $.sum(groceries, function () {
    return this.price;

// total === 13


Release History

(Nothing yet)

Building from source

  • Clone this repository using git clone
  • Install grunt and dependencies using npm install
  • Build the plugin and run tests using the grunt command.
Something went wrong with that request. Please try again.