javascript - 删除多维数组中的重复项
问题描述
我有这些数据,如何返回唯一数组 - 每个索引都没有重复的数组。
[
[ 0, 1, 2 ],
[ 1, 0, 2 ],
[ 1, 1, 1 ],
[ 1, 2, 0 ],
[ 2, 0, 1 ],
[ 2, 1, 0 ]
]
我想要的输出是这样的
0 1 2
1 2 0
2 0 1
解决方案
这应该是你想要的。
console.clear()
arr = [
[ 0, 1, 2 ],
[ 1, 0, 2 ],
[ 1, 1, 1 ],
[ 1, 2, 0 ],
[ 2, 0, 1 ],
[ 2, 1, 0 ]
];
var res = arr.filter((duplicates = [], e => {
// prefill duplicates with empty arrays
e.forEach((k, i) => {
duplicates[i] = duplicates[i] || [];
})
// check if there are duplicates and then set keep to false
let keep = e.reduce((keep, val, i) => {
return keep && (!duplicates[i].includes(val));
}, true);
// if keep, save this array to duplicates
if (keep) {
e.forEach((k, i) => {
duplicates[i].push(k)
})
}
return keep;
}))
res.forEach(k => {
console.log(...k)
})
.as-console-wrapper { max-height: 100% !important; top: 0; }
推荐阅读
- apache-kafka - 处理 kafka 消费者中的反序列化错误(死信队列)
- entity-framework - 添加数据时是否可以确保顺序?
- excel - 如何仅禁用 Excel 电子表格中特定列的填充句柄?
- python - 计算嵌套列表中的最大差异
- html - 如何在使用 Spacy NER 预测命名实体时从文本中删除 html 标签,并再次使用 html 标签以原始格式显示相同的文本?
- d3.js - d3 浮动分组条在时间轴中具有范围值
- c# - 如何强制 MVC 应用程序在启用 Windows 身份验证的情况下要求提供凭据
- javascript - 选择框选项列表更改检测其他选择框更改未检测到更改
- python - 如何在pyautogui中单击图像中心
- vue.js - Vue Nuxt 滚动到不同的页面