Average the values in a data set.

It can sometimes be useful to get the average of data in an API result set, be it from a column, or a collection of cells. This method provides exactly that ability.

Plug-in code

jQuery.fn.dataTable.Api.register( 'average()', function () {
    var data = this.flatten();
    var sum = data.reduce( function ( a, b ) {
        return (a*1) + (b*1); // cast values in-case they are strings
    }, 0 );
    return sum / data.length;
} );


This plug-in is available on the DataTables CDN:


Note that if you are using multiple plug-ins, it is beneficial in terms of performance to combine the plug-ins into a single file and host it on your own server, rather than making multiple requests to the DataTables CDN.

Version control

If you have any ideas for how this plug-in can be improved, or spot anything that is in error, it is available on GitHub and pull requests are very welcome!


// Average a column
  var table = $('#example').DataTable();
  table.column( 3 ).data().average();
// Average two cells
  var table = $('#example').DataTable();
  table.cells( 0, [3,4] ).data().average();