1. /**
  2. * Compares two JSON objects for equality. Handles older JavaScript environments.
  3. *
  4. * @param {object} obj1 The first JSON object.
  5. * @param {object} obj2 The second JSON object.
  6. * @returns {boolean} True if the objects are equal, false otherwise.
  7. */
  8. function compareJsonObjects(obj1, obj2) {
  9. // Handle cases where either input is not an object.
  10. if (typeof obj1 !== 'object' || obj1 === null || typeof obj2 !== 'object' || obj2 === null) {
  11. return obj1 === obj2; // Compare primitive values directly
  12. }
  13. // Check if both are the same object (same memory address)
  14. if (obj1 === obj2) {
  15. return true;
  16. }
  17. // Check if the sizes are different.
  18. if (Object.keys(obj1).length !== Object.keys(obj2).length) {
  19. return false;
  20. }
  21. // Iterate through the keys and compare values.
  22. for (const key in obj1) {
  23. if (obj1.hasOwnProperty(key)) {
  24. if (obj1[key] !== obj2[key]) {
  25. return false;
  26. }
  27. }
  28. }
  29. return true;
  30. }

Add your comment