首页 > 解决方案 > 如何合并两个包含相同键的不同值的对象数组,并将新对象推入数组?

问题描述

我有两个数组:

arr1 = [
  {
    "OwnershipNumber": 0,
    "ID": null,
    "Name": "Contractor LLC",
    "ContrEmployeeTypeId": 0,
    "ContactEmail": "",
    "ContactPhone": "",
    "VeteranEmployeeMilitaryAffiliation": "",
    "SocialSecurityNumber": "",
    "DrivingLicense": "",
    "DateOfBirth": null,
    "OwnershipPercentage": 0,
    "IsContractorActive": "Y",
    "VeteranFlag": "N",
    "VeteranEmployeeHireDate": null,
    "LegalIssueFlag": "N",
    "ActiveFlag": true,
    "TimeStamp": null
  },
  {
    "OwnershipNumber": 1878,
    "ID": null,
    "Name": "Greg Dawson",
    "ContrEmployeeTypeId": 2,
    "ContactEmail": "pra@abc.com",
    "ContactPhone": "455-455-6444",
    "VeteranEmployeeMilitaryAffiliation": null,
    "SocialSecurityNumber": "454534245",
    "DrivingLicense": "44524245",
    "DateOfBirth": "11/30/1968 12:00:00 AM",
    "OwnershipPercentage": 100,
    "IsContractorActive": "Y",
    "VeteranFlag": "N",
    "VeteranEmployeeHireDate": null,
    "LegalIssueFlag": "N",
    "ActiveFlag": true,
    "TimeStamp": null
  }
]


arr 2 = [ {"OwnershipNumber": 1878, "ContactPhone": "111-222-6444"},
 {
    "OwnershipNumber": null,
    "ID": 3,
    "SocialSecurityNumber": "465464654",
    "DrivingLicense": "464654654654",
    "DateOfBirth": "1998-12-12T18:30:00.000Z",
    "VeteranEmployeeHireDate": "1970-01-01T00:00:00.000Z",
    "Name": "Tom Hanks",
    "ContrEmployeeTypeId": 1,
    "IsContractor": "N",
    "ContactEmail": "tom@gmail.com",
    "ContactPhone": "564-465-4654",
    "OwnershipPercentage": 100
  }
]

在合并和推送之后,我正在尝试完成此操作:

arr3 = [
      {
        "OwnershipNumber": 0,
        "ID": null,
        "Name": "Contractor LLC",
        "ContrEmployeeTypeId": 0,
        "ContactEmail": "",
        "ContactPhone": "",
        "VeteranEmployeeMilitaryAffiliation": "",
        "SocialSecurityNumber": "",
        "DrivingLicense": "",
        "DateOfBirth": null,
        "OwnershipPercentage": 0,
        "IsContractorActive": "Y",
        "VeteranFlag": "N",
        "VeteranEmployeeHireDate": null,
        "LegalIssueFlag": "N",
        "ActiveFlag": true,
        "TimeStamp": null
      },
      {
        "OwnershipNumber": 1878,
        "ID": null,
        "Name": "Greg Dawson",
        "ContrEmployeeTypeId": 2,
        "ContactEmail": "pra@abc.com",
        "ContactPhone": "111-222-6444",
        "VeteranEmployeeMilitaryAffiliation": null,
        "SocialSecurityNumber": "454534245",
        "DrivingLicense": "44524245",
        "DateOfBirth": "11/30/1968 12:00:00 AM",
        "OwnershipPercentage": 100,
        "IsContractorActive": "Y",
        "VeteranFlag": "N",
        "VeteranEmployeeHireDate": null,
        "LegalIssueFlag": "N",
        "ActiveFlag": true,
        "TimeStamp": null
      },
{
    "OwnershipNumber": null,
    "ID": 3,
    "SocialSecurityNumber": "465464654",
    "DrivingLicense": "464654654654",
    "DateOfBirth": "1998-12-12T18:30:00.000Z",
    "VeteranEmployeeHireDate": "1970-01-01T00:00:00.000Z",
    "Name": "Tom Smith",
    "ContrEmployeeTypeId": 1,
    "IsContractor": "N",
    "ContactEmail": "tom@gmail.com",
    "ContactPhone": "564-465-4654",
    "OwnershipPercentage": 100
  }
    ]

第一个数组 (arr1) 是主数组,第二个数组 (arr2) 仅包含对第一个数组所做的更改(在此示例中,您可以看到所有权编号 1878 在 ContactPhone 字段中发生了更改,因此 ContactPhone与所有权编号一起出现在 arr2 中,它还可以包含一个新对象,例如具有名称字段 Tom Smith 的对象(此对象在 arr 1 中不存在),我正在尝试根据 OwnershipNumber 和 ContactEmail 合并更改将新元素,即具有名称字段 Tom Smith 的对象添加到新数组 (arr3) 中。新元素的更改和包含是可选的,这意味着存在 arr 2 可能为空白的情况。请帮我解决这个问题。

标签: javascriptarraysangulartypescript

解决方案


我假设这OwnerdhipNumber是一个唯一的键,可以知道您要对哪个对象进行更改(或添加,如果此编号尚不存在)。在这种情况下,以下代码可能适合您:

const arr1 = [{"OwnershipNumber": 0,"ID": null,"Name": "Contractor LLC","ContrEmployeeTypeId": 0,"ContactEmail": "","ContactPhone": "","VeteranEmployeeMilitaryAffiliation": "","SocialSecurityNumber": "","DrivingLicense": "","DateOfBirth": null,"OwnershipPercentage": 0,"IsContractorActive": "Y","VeteranFlag": "N","VeteranEmployeeHireDate": null,"LegalIssueFlag": "N","ActiveFlag": true,"TimeStamp": null},{"OwnershipNumber": 1878,"ID": null,"Name": "Greg Dawson","ContrEmployeeTypeId": 2,"ContactEmail": "pra@abc.com","ContactPhone": "455-455-6444","VeteranEmployeeMilitaryAffiliation": null,"SocialSecurityNumber": "454534245","DrivingLicense": "44524245","DateOfBirth": "11/30/1968 12:00:00 AM","OwnershipPercentage": 100,"IsContractorActive": "Y","VeteranFlag": "N","VeteranEmployeeHireDate": null,"LegalIssueFlag": "N","ActiveFlag": true,"TimeStamp": null}];
const arr2 = [ {"OwnershipNumber": 1878, "ContactPhone": "111-222-6444"},{"OwnershipNumber": null,"ID": 3,"SocialSecurityNumber": "465464654","DrivingLicense": "464654654654","DateOfBirth": "1998-12-12T18:30:00.000Z","VeteranEmployeeHireDate": "1970-01-01T00:00:00.000Z","Name": "Tom Hanks","ContrEmployeeTypeId": 1,"IsContractor": "N","ContactEmail": "tom@gmail.com","ContactPhone": "564-465-4654","OwnershipPercentage": 100}];
arr2.forEach(t => {
  for(const [idx, obj] of arr1.entries()){
    if(obj.OwnershipNumber === t.OwnershipNumber) {
      // The object exist in arr1, need to update the existing object
      arr1[idx] = {...arr1[idx], ...t};
      return;
    }
  }
  // The object not already exist. we should add it to the array
  arr1.push(t);
});
console.log(arr1);


推荐阅读