javascript - 使用 javascript 查找表行的最小值

标签 javascript jquery arrays

好的,目前我正在使用此代码来查找它,但我需要过滤掉空字段,以便所有空白单元格不会变成红色。

$('tr').each(function highlight() {
    var $td = $(this).children('td');

    // find all the values
    var vals = $td.map(function () {
            return +$(this).text();
        }).filter(function (val) {
            return val !== null
        });
}).get();

// then find their minimum
var min = Math.min.apply(Math, vals);

// tag any cell matching the min value
$td.filter(function highlight() {
    return +$(this).text() === min;
}).addClass('alert alert-danger');
});

那么如何过滤掉空值呢?

最佳答案

如果 $('#asd').text() 计算结果为“”

然后 +$('#asd').text() 将计算为 0

<小时/>

查找整个表中的最小值

http://jsfiddle.net/6DgAW/

var vals = $('tr td').map(function () {
    return isNaN(parseInt($(this).text(), 10)) ? parseInt($(this).text(), 10) :  null;
}).get();

// then find their minimum
var min = Math.min.apply(Math, vals);

// tag any cell matching the min value
$('tr td').filter(function () {
    return parseInt($(this).text(), 10) === min;
}).addClass('alert alert-danger');
<小时/>

找出每行的最小值

http://jsfiddle.net/DggUN/18/

$('tr').each(function(){
    var vals = $('td,th',this).map(function () {
        return parseInt($(this).text(), 10) ? parseInt($(this).text(), 10) :  null;
    }).get();
    // then find their minimum
    var min = Math.min.apply(Math, vals);

    // tag any cell matching the min value
    $('td,th', this).filter(function () {
        return parseInt($(this).text(), 10) === min;
    }).addClass('alert alert-danger');
});

关于javascript - 使用 javascript 查找表行的最小值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22470002/

相关文章:

javascript - 将变量设置为 React 组件不起作用

c++ - '->' token 之前预期有不合格的 id,如何解决此问题?

javascript - 对数组进行 n 次过滤

javascript - wp_dequeue_script 子主题替换脚本

javascript - 变量访问的 Angular 和 javascript 范围

javascript - 标记 HTML 以允许右键单击上下文菜单中的 "open in a new tab"选项的正确方法

javascript - 我可以使用 JavaScript 启用/禁用客户端数字锁定吗?

c# - 如何通过jquery find函数获取所有asp隐藏字段控件id?

arrays - 无法附加到数组扩展中

javascript - 主干关系 - 深层嵌套模型/集合