gpt4 book ai didi

javascript - JS 计算重复出现次数

转载 作者:行者123 更新时间:2023-11-30 08:07:24 31 4
gpt4 key购买 nike

我需要查找 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/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com