首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >查找两个嵌套的Javascript对象之间的差异,并将该差异与一些唯一ID一起存储

查找两个嵌套的Javascript对象之间的差异,并将该差异与一些唯一ID一起存储
EN

Stack Overflow用户
提问于 2020-06-27 15:28:36
回答 1查看 116关注 0票数 1

我分享的是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(它有n个对象,我们必须检查包含LegalIssueEntry的对象,它不是一个空数组),它有ContractorLegalIssueID,LegalIssueTypeID作为唯一键,然后向下遍历到LegalIssueEntry(它也有n个对象),它有LegalIssueEntryID,ContractorLegalIssueID作为唯一键,然后到LegalIssueDetail,在其中进行比较,并取出具有差异的键以及一些附加的键"id","type","LegalIssueFieldTypeID","LegalIssueTypeID“。

差异的最终结果应该是这样的。

代码语言:javascript
复制
 {
      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"
                    }
                  ]
                }
              ]
            }
          ]
        }
      ]
    }

在这件事上请帮帮我。

EN

回答 1

Stack Overflow用户

发布于 2020-07-10 01:43:29

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

代码语言:javascript
复制
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)

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62607167

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档