Skip to content

Instantly share code, notes, and snippets.

@oliverjam
Created November 8, 2016 19:08
Show Gist options
  • Save oliverjam/39af8003ea46c418602d23e067edc8b4 to your computer and use it in GitHub Desktop.
Save oliverjam/39af8003ea46c418602d23e067edc8b4 to your computer and use it in GitHub Desktop.
EJ: C4 P4 created by oliverjam - https://repl.it/ESYb/2
// function deepEqual(a, b) {
// if ((typeof a === 'object' && typeof a !== null) && (typeof b === 'object' && typeof b !== null)) {
// if (Object.getOwnPropertyNames(a).length === Object.getOwnPropertyNames(b).length) {
// for (const propA in a) {
// console.log('First loop:' + a[propA]);
// for (const propB in b) {
// // return deepEqual(propA, propB);
// console.log('Second loop:' + b[propB]);
// }
// }
// } else {
// return false;
// }
// } else {
// return a === b;
// }
// }
function deepEqual(a, b) {
if (a === b) return true;
if (a === null || typeof a != "object" || b === null || typeof b != "object") return false;
let propsInA = 0
let propsInB = 0;
for (let prop in a) propsInA += 1;
for (let prop in b) {
propsInB += 1;
if (!(prop in a) || !deepEqual(a[prop], b[prop]))
return false;
}
return propsInA == propsInB;
}
var obj = {here: {is: "an"}, object: 2};
console.log(deepEqual(obj, obj));
// // → true
console.log(deepEqual(obj, {here: 1, object: 2}));
// // → false
console.log(deepEqual(obj, {here: {is: "an"}, object: 2}));
// // → true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment