I have two objects:
Object 1
{
Manufacturer: 'Manufacturer Name',
User_Name: 'HCP First Name',
HCP_Middle_Name: 'HCP Middle Name',
HCP_Last_Name: 'HCP Last Name',
HCP_NPI: 'HCP NPI',
HCP_Credentials: 'HCP Credentials',
HCP_State_License_Number: 'HCP State License Number',
HCP_State_of_Licensure: 'HCP State of Licensure',
HCP_Specialty: 'HCP Specialty',
Teaching_Hospital_Name: 'Teaching Hospital Name',
Teaching_Hospital_Tax_ID: 'Teaching Hospital Tax ID',
Address_1: 'Address 1',
Address_2: 'Address 2',
City: 'City',
State: 'State',
Province: 'Province',
Postal_Code: 'Postal Code',
Country: 'Country',
Spend_Date: 'Spend Date',
Spend_Currency: 'Spend Currency',
Spend_Amount: 'Spend Amount',
Form_of_Payment: 'Form of Payment',
Nature_of_Payment: 'Nature of Payment',
Home_System_Identifier: 'Home System Identifier',
Product_Name: 'Product Name',
Travel_City: 'Travel City',
Travel_State: 'Travel State',
Travel_Country: 'Travel Country'
}
Object 2
[
{
Manufacturer: 'Ascendis',
User_Name: '',
HCP_Middle_Name: '',
HCP_Last_Name: '',
HCP_NPI: '',
HCP_Credentials: '',
HCP_State_License_Number: '',
HCP_State_of_Licensure: '',
HCP_Specialty: '',
Teaching_Hospital_Name: '',
Teaching_Hospital_Tax_ID: '',
Address_1: '',
Address_2: '',
City: '',
State: '',
Province: '',
Postal_Code: '',
Country: '',
Spend_Date: '',
Spend_Currency: '',
Spend_Amount: '',
Form_of_Payment: '',
Nature_of_Payment: '',
Home_System_Identifier: '',
Product_Name: '',
Travel_City: '',
Travel_State: '',
Travel_Country: ''
},
{
Manufacturer: 'Amneal',
User_Name: '',
HCP_Middle_Name: '',
HCP_Last_Name: '',
HCP_NPI: '',
HCP_Credentials: '',
HCP_State_License_Number: '',
HCP_State_of_Licensure: '',
HCP_Specialty: '',
Teaching_Hospital_Name: '',
Teaching_Hospital_Tax_ID: '',
Address_1: '',
Address_2: '',
City: '',
State: '',
Province: '',
Postal_Code: '',
Country: '',
Spend_Date: '',
Spend_Currency: '',
Spend_Amount: '',
Form_of_Payment: '',
Nature_of_Payment: '',
Home_System_Identifier: '',
Product_Name: '',
Travel_City: '',
Travel_State: '',
Travel_Country: ''
}
]
What am I trying to accomplish here is to return a new array of objects by comparing the keys and values in Object 1 with the keys in Object 2. For example, In Object 1, key "Manufacturer" has a value of "Manufacturer Name", so I need to check in Object 2 if the key "Manufacturer" exists and if it does, I need to replace the key of "Manufacturer" in Object 2 with the value in Object 1 (Manufacturer Name).
Please excuse most of the empty values in Object 2 as these are blank for testing purposes, however, the user will need to populate these in prod.
CodePudding user response:
Object.keys(obj1).forEach(key1 => {if (Object.keys(obj2).includes(key1)) { obj2[obj1[key1]] = obj2[key1]; delete obj2[key1]; }})