Find Minimum Value in a Column

html, html-table, javascript, jquery

Solution

Using jQuery, you could use the `.map()` method with the `.get()` method to get an Array of integers, then `.apply()` the Array as the arguments for `Math.min` to get the minimum.

This assumes that you want the first column in the table. Selector may need to change depending on which column you want.

Example: http://jsbin.com/iyiqa3/

var values = $('#myTable tr > td:first-child').map(function() {
    return parseInt( $.text( [this] ) );
}).get();

var minimum = Math.min.apply( null, values );

Without jQuery, try this:

Example: http://jsbin.com/iyiqa3/2/

var values = [];

var trs = document.getElementById('myTable').getElementsByTagName('tr');

for( var i = 0, len = trs.length; i < len; i++ ) {
    values.push( parseInt( trs[ i ].cells[ 0 ].innerHTML ) );
}

var minimum = Math.min.apply( null, values );

Problem

I have an HTML table column containing integers. What's the most efficient way to get the minimum value using JavaScript or JQuery?

Original source