【问题标题】:Finding difference between two nested Javascript Objects and storing the difference along with some unique ID's查找两个嵌套 Javascript 对象之间的差异并将差异与一些唯一 ID 一起存储
【发布时间】:2020-10-17 18:56:00
【问题描述】:

我正在共享 stackblitz url,因为 Javascript 对象很长并且不适合正文。第一个对象(objOne)是原始对象,第二个对象(objTwo)有一些变化。

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

我发现很难遍历并找出一些唯一 ID 的差异。所有权信息列表数组包含两个对象,并有一个唯一的 id 键,然后遍历到 ContractorLegalIssue(它没有对象,我们必须检查包含 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"
                    }
                  ]
                }
              ]
            }
          ]
        }
      ]
    }

请帮帮我。

【问题讨论】:

  • 这能回答你的问题吗? Generic deep diff between two objects
  • @igg 它可能有助于发现差异,但它无助于获得我也需要的唯一 ID 以及差异。那些唯一 ID 没有改变它们的价值,但我需要它们。

标签: javascript arrays angular object data-structures


【解决方案1】:

看看这对你有没有帮助。结果是具有差异的第三个对象。 我必须减小对象的大小才能发布它。

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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-04
    • 1970-01-01
    • 1970-01-01
    • 2019-10-01
    • 2013-06-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多