![](/img/trans.png)
[英]How to merge two objects (associative array) with same key into one object
[英]How to merge two array of objects containing different values of same key, and also push new object into the array?
我有兩個 arrays:
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 中,它還可以包括一個新的 object 就像名稱字段湯姆史密斯的那個(這個在 arr 1 中不存在),我正在嘗試根據 OwnershipNumber 和 ContactEmail 合並更改將新元素(即 object)添加到新數組 (arr3) 中,其名稱字段為 Tom Smith。 新元素的更改和包含是可選的,這意味着存在 arr 2 可能為空白的情況。 請幫我解決這個問題。
我假設OwnerdhipNumber
是一個唯一鍵,可以知道您要在哪個 object 上進行更改(或添加,如果此編號尚不存在)。 在這種情況下,以下代碼可能適合您:
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);
必須有一個unique key
才能對您的數據進行分組。 在這里,我認為它可以OwnershipNumber
您的唯一密鑰。 現在找到解決方案,我們可以使用reduce()
對數據進行分組:
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}]; var result = Object.values([...arr1, ...arr2].reduce((acc, {OwnershipNumber, ...rest})=>( acc[OwnershipNumber] = {...(acc[OwnershipNumber] || {}), ...{OwnershipNumber, ...rest}}, acc ),{})); console.log(result);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.