javascript - Why am I seeing repeated values for both columns on my table even though I'm using a Set?
问题描述
Hey can anyone tell me why I'm getting repeated values for both oppDesc
and contestEntriesAmt
even though I'm using a Set for oppDesc
(contestEntriesAmt
doesn't need to be in a Set because nothing's being repeated)?
Why are both oppDesc
and contestEntriesAmt
repeating? It just doesn't make sense.
Whenever I remove entries.map(() => {...})
, oppDesc
shows up correctly (no repeated values).
const oppDescData = () => {
const dataOppDesc = oppDesc
let desc = [];
dataOppDesc.forEach((oppD) => {desc.push(oppD.oppDescription);});
const dataNumEntries = numEntries
let entries = [];
dataNumEntries.forEach((entry) => {entries.push(entry.SumOfEntries);});
let filteredDesc = new Set(desc);
let oppDescription = [...filteredDesc];
return (
<>
{
oppDescription.map((oppDesc) => {
return entries.map((contestEntriesAmt) => {
return(
<tr>
<td>{oppDesc}</td>
<td>{contestEntriesAmt}</td>
</tr>
);
})
})
}
</>
);
};
解决方案
当您通过在地图中调用地图来交叉两个列表时,您会得到重复项。假设您有两个列表,每个列表 10 项,结果使用您的代码,您将获得 100 个 tr 条目。即使您将使用集合,并且每个数组的每个值都是唯一的 - 这仍然会发生
推荐阅读
- spring - sec:authorize 不适用于带有 hasRole(ROLE_ADMIN) 或其他的 thymeleaf
- java - 将 shell 脚本参数添加到 java 应用程序
- python - 如何使用openpyxl从python中的xlsx文件中读取货币符号?
- azure - Azure SQL Server 防火墙规则使用 PowerShell 添加多个 IP 地址以进行数据库连接
- javascript - Cors 阻止 ajax 请求,尽管 Access-Control-Allow-Origin:*
- java - 有没有办法从 Spring Security 中获取 UserId?
- mongodb - Mongoose - 通过作者的 id 查找模型
- node.js - Node Express + Sequelize findAll 在空数据库上返回 SequelizeDatabaseError
- protractor - 控制窗口或文件资源管理器在量角器中上传文件
- apache-spark - Pyspark 配置处理多个大小超过 10 gb 的 gzip 文件(单个文件大小 ~500 mb)