Last active
August 29, 2015 14:21
-
-
Save DawnPaladin/5b426f5a31731685199c to your computer and use it in GitHub Desktop.
Annotated recursive object comparison
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Deep comparison | |
The == operator compares objects by identity. But sometimes, you would prefer to compare the values of their actual properties. | |
Write a function, deepEqual, that takes two values and returns true only if they are the same value or are objects with the same properties whose values are also equal when compared with a recursive call to deepEqual. | |
To find out whether to compare two things by identity (use the === operator for that) or by looking at their properties, you can use the typeof operator. If it produces "object" for both values, you should do a deep comparison. But you have to take one silly exception into account: by a historical accident, typeof null also produces "object". | |
Hints: | |
Your test for whether you are dealing with a real object will look something like typeof x == "object" && x != null. Be careful to compare properties only when both arguments are objects. In all other cases you can just immediately return the result of applying ===. | |
Use a for/in loop to go over the properties. You need to test whether both objects have the same set of property names and whether those properties have identical values. The first test can be done by counting the properties in both objects and returning false if the numbers of properties are different. If they’re the same, then go over the properties of one object, and for each of them, verify that the other object also has the property. The values of the properties are compared by a recursive call to deepEqual. | |
Returning the correct value from the function is best done by immediately returning false when a mismatch is noticed and returning true at the end of the function. | |
*/ | |
function deepEqual(alpha, bravo) { | |
console.log("Checking", alpha, bravo); | |
if (typeof alpha == "object" && typeof bravo == "object" && alpha != null && bravo != null) { | |
var propertyCountAlpha = 0; | |
for (var prop in alpha) { | |
propertyCountAlpha++; | |
} | |
var propertyCountBravo = 0; | |
for (var prop in bravo) { | |
propertyCountBravo++; | |
} | |
if (propertyCountAlpha !== propertyCountBravo) | |
return false; | |
else { | |
console.log("Property counts equal"); | |
for (var prop in alpha) { | |
if(deepEqual(alpha[prop], bravo[prop])) { | |
console.log(prop,"properties equal"); | |
} else { | |
return false; | |
} | |
} | |
} | |
console.log("All properties equal"); | |
return true; | |
} else { | |
if (alpha === bravo) { | |
console.log(alpha, bravo, "equal"); | |
return true; | |
} | |
else { | |
console.log(alpha, bravo, "not equal"); | |
return false; | |
} | |
} | |
} | |
console.log("\n Round 1"); | |
var obj = {here: {is: "an"}, object: 2}; | |
console.log(deepEqual(obj, obj)); | |
// → true | |
console.log("\n Round 2"); | |
console.log(deepEqual(obj, {here: 1, object: 2})); | |
// → false | |
console.log("\n Round 3"); | |
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