2

I am sharing stackblitz url as the Javascript Objects are quite long and won't fit in the body. The first object(objOne) is the original one and the second object(objTwo) has some changes in it.

URL Click here (compare two Javascript Objects and find difference and add unique keys along with it)

I am finding it difficult to traverse and track out the difference along with some unique ID's. The Ownership Information List array contains two objects and has a unique id key to them then traversing down to ContractorLegalIssue( it has n no of objects and we have to check the object containing LegalIssueEntry which is not an empty array), it has ContractorLegalIssueID, LegalIssueTypeID as unique key then traversing down to LegalIssueEntry(it also has n no of objects) , it has LegalIssueEntryID, ContractorLegalIssueID as unique key and then to LegalIssueDetail in which the comparison with take place and take out the keys which have the difference along with some additional keys "id", "type","LegalIssueFieldTypeID","LegalIssueTypeID".

The Final result of the difference should be like this.

 {
      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"
                    }
                  ]
                }
              ]
            }
          ]
        }
      ]
    }

Please help me out in this.

Pratyush Swain
  • 144
  • 1
  • 9
  • Does this answer your question? [Generic deep diff between two objects](https://stackoverflow.com/questions/8572826/generic-deep-diff-between-two-objects) – igg Jun 27 '20 at 08:29
  • @igg It might help in finding difference but it won't help in getting the unique id's that I also need along with the difference.Those unique id's have not changed their value but I need them. – Pratyush Swain Jun 27 '20 at 09:04

1 Answers1

0

See if this helps you. The result is a third object with the differences. I had to reduce the size of the objects to posted it.

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)