我分享的是stackblitz url,因为Javascript对象太长了,不适合放在正文中。第一个对象(ObjOne)是原始对象,第二个对象(ObjTwo)对其进行了一些更改。
我发现很难遍历和跟踪其中的差异以及一些唯一的id。所有权信息列表数组包含两个对象,并对它们有一个唯一的id键,然后向下遍历到ContractorLegalIssue(它有n个对象,我们必须检查包含LegalIssueEntry的对象,它不是一个空数组),它有ContractorLegalIssueID,LegalIssueTypeID作为唯一键,然后向下遍历到LegalIssueEntry(它也有n个对象),它有LegalIssueEntryID,ContractorLegalIssueID作为唯一键,然后到LegalIssueDetail,在其中进行比较,并取出具有差异的键以及一些附加的键"id","type","LegalIssueFieldTypeID","LegalIssueTypeID“。
差异的最终结果应该是这样的。
{
difference: [
{
id: 1,
ContractorLegalIssue: [
{
ContractorLegalIssueID: 1597,
LegalIssueTypeID: 1,
LegalIssueEntry: [
{
LegalIssueEntryID: 1151,
ContractorLegalIssueID: 1597,
LegalIssueDetail: [
{
id: 2,
value: "changed pos1",
"type": "Text",
"LegalIssueFieldTypeID": 2,
"FieldDetailText": "changed pos1",
"LegalIssueTypeID": 1
}
]
}
]
}
]
},
{
id: 2,
ContractorLegalIssue: [
{
ContractorLegalIssueID: 1599,
LegalIssueTypeID: 2,
LegalIssueEntry: [
{
LegalIssueEntryID: 1154,
ContractorLegalIssueID: 1599,
LegalIssueDetail: [
{
"id": 9,
"value": "4",
"type": "INT",
"LegalIssueTypeID": 2,
"LegalIssueFieldTypeID": 9,
"FieldDetailInt": 4
},
{
"id": 10,
"value": "changed for owner",
"type": "Text",
"LegalIssueTypeID": 2,
"LegalIssueFieldTypeID": 10,
"FieldDetailText": "changed for owner"
}
]
}
]
}
]
}
]
}在这件事上请帮帮我。
发布于 2020-07-10 01:43:29
看看这对你有没有帮助。结果是具有差异的第三个对象。我必须减小对象的大小才能发布它。
let objOne =
{ OwnershipDetails:{
"OwnershipInformationList": [
{
"OwnershipNumber": 0,
"ID": 1,
"ContractorLegalIssue": [
],
"TimeStamp": null
},
{
"OwnershipNumber": 1878,
"ID": 2,
"ContractorLegalIssue": [
{
"ContractorLegalIssueID": null,
"LegalIssueTypeID": 1,
"LegalIssueEntry": [],
"ActiveFlag": null,
"RemoveDate": null,
"RemoveResourceID": null,
"CreatedDate": null,
"CreatedResourceID": null,
"LegalIssueType": "LITIGATIONS",
"OwnershipNumber": 1878,
"Name": "Harvey Smith ",
"LegalIssueFlg": "Y"
}
],
"TimeStamp": null
}
]
}
};
let objTwo =
{OwnershipDetails:{
"OwnershipInformationList": [
{
"OwnershipNumber": 0,
"ID": 1,
"ContractorLegalIssue": [
{
"ContractorLegalIssueID": null,
"LegalIssueTypeID": 4,
"LegalIssueEntry": [],
"ActiveFlag": null,
"RemoveDate": null,
"RemoveResourceID": null,
"CreatedDate": null,
"CreatedResourceID": null,
"LegalIssueType": "ALIASES",
"OwnershipNumber": 0,
"Name": "New company ",
"LegalIssueFlg": "Y"
}
],
"TimeStamp": null
},
{
"OwnershipNumber": 1878,
"ID": 2,
"ContractorLegalIssue": [
],
"TimeStamp": null
}
]
}
};
const iterate = (obj, tracking = '', trackingObject = {}) => {
for (let property in obj) {
if (obj.hasOwnProperty(property)) {
if (typeof obj[property] == "object") {
trackingObject[tracking] = "object";
iterate(obj[property], tracking + '.' + property, trackingObject);
} else {
trackingObject[tracking] = obj[property]
}
}
}
return trackingObject
};
// getting the objects paths (ordered)
const arrayPathsOne = Object.keys(iterate(objOne));
const arrayPathsTwo = Object.keys(iterate(objTwo));
const [iterateOver, secondary] = arrayPathsOne.length > arrayPathsTwo ? [arrayPathsOne, arrayPathsTwo] : [arrayPathsTwo, arrayPathsOne];
let differences = [];
// getting the differences paths
for (let i in iterateOver) {
if (iterateOver.hasOwnProperty(i)) {
if (secondary.hasOwnProperty(i)) {
if (iterateOver[i] !== secondary[i]) {
differences.push(iterateOver[i])
}
} else {
differences.push(iterateOver[i])
}
}
}
let obj = {};
// building and filling the result object with the differences
for (let i in differences) {
if (differences.hasOwnProperty(i)) {
let path = differences[i].split('.');
// removing first element === ''
path.splice(0, 1);
const lastKey = path.pop();
// getting object build from the path
let pathObj = path.reduce((obj, key) => obj[key] = obj[key] || {}, obj);
// getting the values
const obj1 = path.reduce((obj, key) => obj[key] = obj[key] || {}, objOne);
const obj2 = path.reduce((obj, key) => obj[key] = obj[key] || {}, objTwo);
// filling the path object value
// this can have limitation because could be equal objects with different array index, like obj1 = {[{a: '1'}, {b: '2'}]} and obj2 = {[{b: '2'}, {a: '1'}]}
pathObj[lastKey] = obj1[lastKey] || obj2[lastKey];
}
}
console.log(obj)
https://stackoverflow.com/questions/62607167
复制相似问题