查找列中的最小值 [英] Find Minimum Value in a Column

查看:33
本文介绍了查找列中的最小值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含整数的 HTML 表格列.使用 JavaScript 或 JQuery 获得最小值的最有效方法是什么?

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

推荐答案

使用 jQuery,你可以使用.map() 方法.get() 方法 获取整数数组,然后是 .apply() 数组作为参数 Math.min 得到最小值.

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.

示例: 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 );

没有 jQuery,试试这个:

Without jQuery, try this:

示例: 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 );

这篇关于查找列中的最小值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆