【发布时间】:2020-10-27 01:35:27
【问题描述】:
我需要创建一个函数来仅显示来自第一个重复对象的重复数据。如果您查看我的代码,我有一个数组,其中包含对象。我的数据结构是这样的:
[
{ data: ["22409972", "1267304", "1000", "FG-18-1267304"] },
{ data: ["22409972", "1267304", "2000", "FG-18-1267304"] }
]
在这种情况下,我不想包含第二个数据对象,因为它的第一个索引已经存在于第一个对象“22409972”中
我需要一个可以支持多个具有相同第一个索引的重复对象的解决方案,但仍然只想返回包含重复索引的第一个对象
基本上如果存在重复对象,则将其从 arrayThatContainsDuplicates 数组中删除
我试图用这个作为解决方案
const noDuplicates = [];
for (let i = 0; i < duplicateRows.length; i++) {
if (i + 1 < duplicateRows.length) {
if (duplicateRows[i].data[0] !== duplicateRows[i + 1].data[0]) {
noDuplicates.push(duplicateRows[i]);
}
}
The issue with this code is the duplicates are still being returned in the noDuplicates array
【问题讨论】:
标签: javascript arrays object filter