我编写了一个具有基本情况的递归函数,但由于 if 语句读取集合中的值,因此它不会终止。 timestamps.has()
始终返回 false
,即使它“应该”返回 true
。
我无法追踪该错误,并且想知道我是否做错了一些简单的错误,例如 timestamps
变量的范围,我已经检查了集合中的值并且所比较的值实际上是相等的,所以我不确定出了什么问题。
var timestamps;
trackFraud = async () => {
account = document.getElementById("account").value;
timestamps = new Set();
await fraudClimb(account, 0);
async function fraudClimb(account, theseTimestamps) {
var [frauds, theseTimestamps] = await findFraudByFromAccount.call(this, account, theseTimestamps);
if (frauds.length == 0) return;
for (var i = 0; i < frauds.length; i++) {
await fraudClimb(frauds[i], theseTimestamps[i]);
}
}
}
findFraudByFromAccount = async (account, timestamp) => {
events = await window.KYCinstance.getPastEvents('ReportedFraudB', { filter: {fromAccount: web3.utils.fromAscii(account)}, fromBlock: 0 });
var frauds = [];
var theseTimestamps = [];
for (let i = 0; i < events.length; i++) {
let values = events[i].returnValues;
if (timestamps.has(values.times)) continue; //THIS LINE IS NEVER FAILING
else {
timestamps.add(values.time);
theseTimestamps.push(values.time);
frauds.push(web3.utils.toAscii(values.fromAccount));
}
}
return [frauds, theseTimestamps];
}
最佳答案
看起来您在这里不匹配:
extra 's'
/
if (timestamps.has(values.times)) continue;
else {
timestamps.add(values.time);
\
no 's' here
...
根据您的 values
对象结构,这可能至关重要。如果这些属性不相等或其中之一不存在,您的 has
检查将返回 false
。
关于javascript - 当项目存在时,JS Set.has() 返回 false bool 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55362331/