Reputation: 154
I have two 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": "[email protected]",
"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": "[email protected]",
"ContactPhone": "564-465-4654",
"OwnershipPercentage": 100
}
]
I am trying to accomplish this after merging and pushing:
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": "[email protected]",
"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": "[email protected]",
"ContactPhone": "564-465-4654",
"OwnershipPercentage": 100
}
]
The first array (arr1) is the master array, the second array (arr2) contains only the changes that has been done with respect to the first array (in this example you can see ownership number 1878 has a change in ContactPhone field, therefore ContactPhone along with OwnershipNumber is present in arr2, it can also include a new object like the one with name field Tom Smith (this one doesn't exist in arr 1), I am trying to merge the the change on basis of OwnershipNumber and ContactEmail and add the new element i.e. the object which has the name field Tom Smith into the new array (arr3)). The change and inclusion of new elements is optional which means there is a case where arr 2 maybe blank. Please help me out on this.
Upvotes: 0
Views: 73
Reputation: 5308
There must be a unique key
in order to group your data. Here I think it could OwnershipNumber
your unique key. Now coming to the solution, we can make use of reduce()
to group the data:
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": "[email protected]","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": "[email protected]","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);
Upvotes: 0
Reputation: 5895
I assuming the OwnerdhipNumber
is a unique key to know on which object you want to make changes (or add, if this number not exist yet). In this case the below code meight work for you:
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": "[email protected]","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": "[email protected]","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);
Upvotes: 1