我有一个对象,我想从其属性之一中删除元素,该属性是基于外部对象的匹配属性的对象数组。
这使用npm deep-diff比较两个对象。
我的问题是在CombineDuplicateRecords内部,它将每个记录与每个记录进行比较,从而在identities数组中创建重复项。因此身份最终将看起来像:
[{
id: "111",
identities: [
{
id: "111"
},
{
id: "111"
},
{
id: "222"
},
{
id: "222"
},
{
id: "333"
},
{
id: "333"
}
]
}]
当我真的希望它看起来像这样时:
[{
id: "111",
identities:[
{
id: "222"
},
{
id: "333"
}
]
}]
代码:
var requestRecords = [
{
vid: "12345",
id: "12345",
email: "gft@test.com",
firstName: "GrandFathering",
lastName: "TestMN",
postalCode: "55443-2410",
phone: "123-456-7890",
key: "1212"
},
{
vid: "121212",
id: "12222",
email: "g233@test.com",
firstName: "NoMatch",
lastName: "NoMatchFound",
postalCode: "43233-2410",
phone: "123-456-7890",
key: "121233"
},
{
vid: "111",
id: "111",
email: "ffffft@test.com",
firstName: "samebatch",
lastName: "samebatch",
postalCode: "5545",
phone: "123-456-7890",
key: "3333",
},
{
vid: "222",
id: "222",
email: "ffffft@test.com",
firstName: "samebatch",
lastName: "samebatch",
postalCode: "5545",
phone: "123-456-7890",
key: "4444",
},
{
vid: "333",
id: "333",
email: "ffffft@test.com",
firstName: "samebatch",
lastName: "samebatch",
postalCode: "5545",
phone: "123-456-7890",
key: "55",
}
];
combineDuplicateRecords = (arrayOfRecords, prefilter) => {
const recordsToRemove = [];
arrayOfRecords.forEach(firstRecord => {
arrayOfRecords.forEach((secondRecord, index) => {
if (
firstRecord.firstName == secondRecord.firstName &&
firstRecord.lastName == secondRecord.lastName &&
firstRecord.dateOfBirth == secondRecord.dateOfBirth &&
firstRecord.phone == secondRecord.phone &&
firstRecord.postalCode == secondRecord.postalCode &&
firstRecord.id !=
secondRecord.id
) {
const identities = [];
let identity = {};
this.preserveExisitingIdentities(secondRecord, identities);
this.preserveExisitingIdentities(firstRecord, identities);
identity = this.setIdentityDifferencesBetweenRecords(
firstRecord,
secondRecord,
prefilter,
identity
);
identities.push(identity);
firstRecord["identities"] = identities;
recordsToRemove.push(index);
}
});
});
[...new Set(recordsToRemove)].forEach(index => {
arrayOfRecords.splice(index, 1);
});
return arrayOfRecords;
};
preserveExisitingIdentities = (record, identities) => {
if (record.hasOwnProperty("identities")) {
record.identities.forEach(identity => {
identities.push(identity);
});
}
return identities;
};
setIdentityDifferencesBetweenRecords = (
firstIdentity,
secondIdentity,
prefilter,
identity
) => {
const differences = Diff(firstIdentity, secondIdentity, prefilter);
let i = differences.length;
while (i--) {
if (differences[i].path[0] == "vid") {
differences.splice(i, 1);
}
if (differences[i].path[0] == "identities") {
differences.splice(i, 1);
}
//we only want to keep the differences so we remove kind D
if (differences[i]?.kind == "D") {
differences.splice(i, 1);
}
}
differences.forEach(diff => {
identity[diff.path[0]] = diff.lhs;
});
return identity;
};
console.log(JSON.stringify(combineDuplicateRecords(requestRecords)));
答案 0 :(得分:1)
抓住每个内部ID并将其保存在数据结构中,然后使用Array#find
查找整个对象并将其重新插入identities
const array = [
{
id: "111",
identities: [
{
id: "111"
},
{
id: "111"
},
{
id: "222"
},
{
id: "222"
},
{
id: "333"
},
{
id: "333"
}
]
}
]
const cleanObject = (obj) => {
const allIds = obj.identities.map(({ id }) => id)
const mainId = obj.id
const uniqueIds = new Set(allIds)
uniqueIds.delete(mainId)
const nextIdentities = [...uniqueIds].map(currId => {
return obj.identities.find(({ id }) => currId === id)
})
obj.identities = nextIdentities
return obj
};
const el = array.map(entry => {
return cleanObject(entry)
})
console.log(el)