/**
* Compares two JSON objects for equality. Handles older JavaScript environments.
*
* @param {object} obj1 The first JSON object.
* @param {object} obj2 The second JSON object.
* @returns {boolean} True if the objects are equal, false otherwise.
*/
function compareJsonObjects(obj1, obj2) {
// Handle cases where either input is not an object.
if (typeof obj1 !== 'object' || obj1 === null || typeof obj2 !== 'object' || obj2 === null) {
return obj1 === obj2; // Compare primitive values directly
}
// Check if both are the same object (same memory address)
if (obj1 === obj2) {
return true;
}
// Check if the sizes are different.
if (Object.keys(obj1).length !== Object.keys(obj2).length) {
return false;
}
// Iterate through the keys and compare values.
for (const key in obj1) {
if (obj1.hasOwnProperty(key)) {
if (obj1[key] !== obj2[key]) {
return false;
}
}
}
return true;
}
Add your comment