jquery - 从jquery中的列表中省略重复项
问题描述
使用 jquery,给出以下列表:
<div>cat</div>
<div>dog</div>
<div>horse</div>
<div>cat</div>
<div>goat</div>
<div>mouse</div>
我们如何遍历它们并省略 -hide()
或remove()
- 重复,即。“猫” ?
解决方案
鉴于,
<div class="my-list">
<div>cat</div>
<div>dog</div>
<div>horse</div>
<div>cat</div>
<div>goat</div>
<div>mouse</div>
</div>
那么你也能:
const $list = $('.my-list');
const showed = [];
$list.find('div').each(function() {
if ( $.inArray($(this).text(), showed) === -1 ) {
showed.push($(this).text());
} else {
$(this).hide(); // or .remove()
}
});
推荐阅读
- rust - 具有未实现 Copy 特征的输入的基准函数
- python - Pyspark:将列从字符串数字转换为时间戳类型
- excel - 从错误的单元格开始访问 VBA 导出到 Excel
- java - liquibase-maven-plugin 与 testcontainers 的使用
- python - 无法使用 Python 将文件上传到 Azure Notebooks 中 Azure Datalake Gen 2 中容器中的目录
- windows - Postgresql:端口 5432 上的连接被拒绝
- kubernetes - Kubernetes 没有为入口分配地址
- laravel-5 - Laravel:没有默认值
- c# - JumpTask.IconResourcePath 的几何
- r - R igraph:根据顶点 ID 或名称为节点着色