Last active
August 29, 2015 14:22
-
-
Save StevenXL/cfb05c5ee35ea77a499e to your computer and use it in GitHub Desktop.
Eloquent JavaScript
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
// compare all values of two objects | |
function deepEqual(first, second) { | |
if (typeof first === "object" && typeof second === "object") { | |
// iterate over properties of both objects | |
for (var property in first) { | |
var firstProperty = first[property]; | |
var secondProperty = second[property]; | |
return deepEqual(firstProperty, secondProperty); | |
} | |
} | |
else if (first !== second) { | |
return false; | |
} | |
return true; | |
} | |
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