a tool for shared writing and social publishing
at main 713 B view raw
1export function deepEquals(obj1: any, obj2: any): boolean { 2 // Check if both are the same reference 3 if (obj1 === obj2) return true; 4 5 // Check if either is null or not an object 6 if ( 7 obj1 === null || 8 obj2 === null || 9 typeof obj1 !== "object" || 10 typeof obj2 !== "object" 11 ) 12 return false; 13 14 // Get keys from both objects 15 const keys1 = Object.keys(obj1); 16 const keys2 = Object.keys(obj2); 17 18 // Check if they have the same number of keys 19 if (keys1.length !== keys2.length) return false; 20 21 // Check each key and value recursively 22 for (const key of keys1) { 23 if (!keys2.includes(key)) return false; 24 if (!deepEquals(obj1[key], obj2[key])) return false; 25 } 26 27 return true; 28}