Skip to content

Instantly share code, notes, and snippets.

@rakin92
Created September 5, 2019 05:29
Show Gist options
  • Save rakin92/66bff36a94c61874ce6ef198ec58ce8d to your computer and use it in GitHub Desktop.
Save rakin92/66bff36a94c61874ce6ef198ec58ce8d to your computer and use it in GitHub Desktop.
const deepEqual = function (x, y) {
if (x === y) {
return true;
}
else if ((typeof x == "object" && x != null) && (typeof y == "object" && y != null)) {
if (Object.keys(x).length != Object.keys(y).length)
return false;
for (let prop in x) {
if (y.hasOwnProperty(prop))
{
if (! deepEqual(x[prop], y[prop]))
return false;
}
else
return false;
}
return true;
}
else
return false;
}
const obj = {here: {is: "an", other: "3"}, 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})); // → false
console.log(deepEqual(obj, {here: {is: "an", other: "2"}, object: 2})); // → false
console.log(deepEqual(obj, {here: {is: "an", other: "3"}, object: 2})); // → true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment