[英]Remove Elements of an array on an object based on outer object property
我有一个对象,我想从其属性之一中删除元素,该属性是基于外部对象的匹配属性的对象数组。
这使用 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)));
获取每个内部 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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.