Skip to content

Instantly share code, notes, and snippets.

@StevenXL
Last active August 29, 2015 14:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save StevenXL/cfb05c5ee35ea77a499e to your computer and use it in GitHub Desktop.
Save StevenXL/cfb05c5ee35ea77a499e to your computer and use it in GitHub Desktop.
Eloquent JavaScript
// 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