I need to compare the values of two objects, obj1 and obj2, by ignoring keys that are missing in either object. If all key-value pairs are equal, return false; otherwise, return true.
For example: If 'id' is present in obj1 but not in obj2, it should be ignored during comparison.
Is there a more efficient way to compare values between two objects with minimal code?
Below is my attempted code snippet
compareResponseAndFormValue(){
const result = JSON.stringify(this.obj1) === JSON.stringify(this.obj2);
console.log(result,"result")
}
Expected output:
The value of the 'bankHoliday' column differs between obj1 and obj2, so the comparison should return true.
Sample response:
const obj1 = {
"id": "",
"uom": "OOOUnits",
"classifiedHours": 720,
"entryType": "Monthly",
"entryDetails": {
"month": "April",
"year": 2024
},
"dataSource": "MDCS",
"availableTime": {
"bankHoliday": "8"
},
"valueOperatingTime": {
"breakDownTime": null
},
"operatingTime": {
"maintenanceTime": 0
},
"loadingTime": {
"nominalSpeed": "130"
},
"oeeCalculations": {
"others": {
"totalTime": 720
}
}
}
const obj2 =
{
"lineId": "E_A571_D",
"classifiedHours": null,
"entryDetails": {
"month": "April",
"year": 2024
},
"availableTime": {
"bankHoliday": "6.05"
},
"valueOperatingTime": {
"breakDownTime": null
},
"operatingTime": {
"maintenanceTime": 0
},
"loadingTime": {
"nominalSpeed": "130"
},
"oeeCalculations": {
"others": {
"totalTime": 720
}
},
"uom": "OOOUnits"
}