Search code examples
javascriptangulartypescriptangular8

How to compare values of two JSON object Angular 8


I have two objects obj1 and obj2 with key and value pairs, I want to compare the values of obj1 and obj2, and if both the objects are equal than it return false else true.

Note : Some keys are missing between obj1 and obj2, so need to ignore those keys.

For ex - id is present in obj1 but not in obj2, so ignore this key.

Can anyone suggest better approach with less code for value comparison between two objects.?

Below code i tried

compareResponseAndFormValue(){   
    const result = JSON.stringify(this.obj1) === JSON.stringify(this.obj2);
    console.log(result,"result")
    
  }

Expected output

In the below response,value of bankHoliday column is different between both obj1 and obj2, so it should return true

Below is my 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",  
}

Solution

  • Sure, here's a custom function that compares the values of two objects, ignoring keys that are not present in both objects. (Returns true if there is any difference).

     function compareObjects(obj1, obj2) {
        for (let key in obj1) {
          if (obj2.hasOwnProperty(key)) {
            if (typeof obj1[key] === 'object' && obj1[key] !== null && obj2[key] !== null) {
              if (compareObjects(obj1[key], obj2[key])) {
                return true;
              }
            } else if (obj1[key] !== obj2[key]) {
              return true;
            }
          }
        }
        return false;
      }
    
    1. This function iterates over the keys of obj1. For each key, it checks if the key exists in both objects.
    2. If the key exists in both, it checks if the corresponding values are objects. If they are, it calls itself recursively to compare these nested objects.
    3. If the values are not objects, or are null, it compares them directly.
    • If it finds any pair of values that are not equal, it returns true.
    • If it finishes checking all keys and hasn't returned true, it returns false, indicating that the objects are equal (ignoring keys that are not present in both objects).