javascript - JS 计算重复出现次数

标签 javascript jquery logic

我需要查找 td.aws 中的字符串是否出现超过 3 次,如果是,则将该字符串放入新列表中。

我有一个这样的表:

<table width="100%" cellspacing="0" cellpadding="2" border="1" class="aws_data">
<tbody><tr bgcolor="#ECECEC"><th>URL (1,908)</th></tr>
<tr><td class="aws">/images/bullet3.png</td></tr>
<tr><td class="aws">/pdf-signing-tool/ErrorCode.properties</td></tr>
<tr><td class="aws">/pdf-signing-tool/Display.properties</td></tr>
<tr><td class="aws">/evcert.cfm</td></tr>
<tr><td class="aws">/evcert.cfm</td></tr>
<tr><td class="aws">/evcert.cfm</td></tr>
<tr><td class="aws">/evcert.cfm</td></tr>
<tr><td class="aws">/repository/03</td></tr>
<tr><td class="aws">/repository/0</td></tr>
etc

<div id="problems"></div>

到目前为止我有:

$('.aws').each(function(){
var temp = $(this).text();
var count = temp.match('/'+temp+'/g');  

if (count.length > 3)
{
    thisString = $(this).text();
    $('#problems').append(thisString)
}

});

谁能帮忙,目前我只是收到 JS 错误“count is null”

JS FIDDLE

最佳答案

Example

//store the counts for each "text" occurrence in a hash table
var countHash = {}; 

//iterate over your tds
$('.aws').each(function(){

    //pull of the text
    var temp = $(this).text();

    //has it already been added to the list? 
    //see: 'countHash[temp] = false;' below.
    if(countHash[temp] === false){return;}

    //increment the occurrence count
    //or set to 1 if this is the first occurrence.
    countHash[temp] = (countHash[temp] || 0) + 1; 

    //have more than three been found?
    if (countHash[temp] > 3)
    {
        //add to your list
        $('#problems').append(temp);

        //ignore future occurrences
        countHash[temp] = false; 
    }
});

关于javascript - JS 计算重复出现次数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15971147/

相关文章:

javascript - 在 WKWebView iOS 中加载本地 React 应用程序

javascript - jQuery cookie 弹出窗口每个 session 仅显示一次

jquery - Slimbox2 适合窗口 - CSS, jQuery

ios - 获取使用 Logic 生成的 midi 序列 (MusicSequence) 标记

javascript - 如何获得两个时刻之间的日历周差?

mysql - 连接 2 个表,当连接行发生时从第二个表打印数据

javascript - 如何创建 makefile 来编译 JavaScript?

javascript - 通过 javascript/jquery 从 midi Controller 读取输入

javascript - Three.js - 如何旋转对象以注视一个点并朝向另一个点

javascript - 使用 ASP.NET WebForms 的 jQuery DataTables 服务器端处理