简体   繁体   English

在html数据表中找到3个最高的数字并将其标记为红色

[英]Finding 3 highest numbers in html data table and marking them red

Problem description: 问题描述:

I have data coming in that shows the electricity consumption of various buildings. 我收到的数据显示了各种建筑物的用电量。 These are in the form of html tables and may, and probably will, contain duplicates. 这些是html表的形式,可能并且可能会包含重复项。

Using jQuery, how do I pick out the 3 highest numbers and mark them by making the background red? 使用jQuery,我如何挑选出3个最高的数字并通过将背景设为红色来标记它们?

Note: it's the three individually highest numbers that I want highlighted/marked red. 注意:这是我要突出显示/标记为红色的三个最高数字。

So if the in-data for example gives me 190, 180, 180, 170, 150 etc. I would like to highlight 190, 180 IN ONE PLACE/CELL, and 170. 因此,例如,如果in-data给我190、180、180、170、150等。我想突出显示190、180 IN ONE PLACE / CELL和170。

Also, I wish to treat the tables as if I have only read-rights, not changing the table in any way except for the highlighting of the three highest numbers. 另外,我希望将表视为只有阅读权限,除了突出显示三个最高数字以外,不以任何方式更改表。

Psuedo code: 伪代码:

var high = [],
    done = 0,
    final = [];
$("td").each(function(){
    high.push({ cell : $(this), val : parseInt($(this).text())});
});

// Now we got all cells sorted DESC
high.sort(function(a, b){
    return b.val - a.val;
});

for(var i=0;i<high.length &&  done < 3;i++){ 
    // Max highest 3
    if( $.inArray(high[i].val, final) == -1 ) {
        final.push(high[i].val);
        done++;
    }
    high[i].cell.addClass("red");
}

Alternative psuedo code 备用伪代码

var final = [],
    done = 0,
    items = $("td").sort(function(a, b){
        return parseInt($(b).text()) - parseInt($(a).val());
    });

items.each(function(){
    var val = parseInt($(this).val());
    if( $.inArray(val), final) == -1 ) {
        done++;
        final.push(val);
    }
    if( done == 3 ) {
        return false; // Stop after we got enough
    }
    $(this).addClass("red");
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM