Created
September 5, 2019 05:29
-
-
Save rakin92/66bff36a94c61874ce6ef198ec58ce8d to your computer and use it in GitHub Desktop.
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
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