Last active
March 22, 2022 20:31
-
-
Save jonurry/d92265f98b0322a5e994894f076c2b2c to your computer and use it in GitHub Desktop.
4.4 Deep Comparison (Eloquent JavaScript Solutions)
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
function deepEqual(a, b) { | |
if (a === b) { | |
// items are identical | |
return true; | |
} else if (typeof a === 'object' && a !== null && typeof b === 'object' && b !== null) { | |
// items are objects - do a deep property value compare | |
// join keys from both objects together in one array | |
let keys = Object.keys(a).concat(Object.keys(b)); | |
// filter out duplicate keys | |
keys = keys.filter( | |
function (value, index, self) { | |
return self.indexOf(value) === index; | |
} | |
); | |
for (p of keys) { | |
if (typeof a[p] === 'object' && typeof b[p] === 'object') { | |
if (deepEqual(a[p], b[p]) === false) { | |
return false; | |
} | |
} else if (a[p] !== b[p]) { | |
return false; | |
} | |
} | |
return true; | |
} else { | |
return false; | |
} | |
} | |
// Tests | |
let 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 | |
console.log(deepEqual(obj, {here: {is: "an"}, object: 0})); | |
// → false | |
console.log(deepEqual(obj, {here: {is: "another"}, object: 2})); | |
// → false | |
console.log(deepEqual(obj, {here: {isnt: "an"}, object: 2})); | |
// → false | |
console.log(deepEqual(obj, {here: {is: "an", deep: {poop: null, value: -1}}, object: 2})); | |
// → false | |
let json = '{"candidate": "Jon Urry", "job": "Junior Full-Stack JavaScript Developer", "portfolio": "jon.urry.me", "skills": ["HTML", "CSS", "JavaScript", "ES6", "Node", "React", "Vue", "Git", "XML", "UX", "Responsive Design", "Design Patterns", "TDD", "Chrome Inspector", "Agile", "SEO", "Analytics", "WordPress", "Databases"], "contact": [{"email": "mailto:jon@urry.me"}, {"github": "github.com/jonurry"}, {"linkedin": "linkedin.com/in/jonurry"}, {"mobile": "tel:+44-7986-371-299"}, {"skype": "skype:jonurry"}, {"twitter": "twitter.com/jonurry"}]}'; | |
let json2 = '{"candidate": "Jon Urry", "job": "Junior Full-Stack JavaScript Developer", "portfolio": "jon.urry.me", "skills": ["HTML", "CSS", "JavaScript", "ES6", "Node", "React", "Vue", "Git", "XML", "UX", "Responsive Design", "Design Patterns", "TDD", "Chrome Inspector", "Agile", "SEO", "Analytics", "WordPress", "Databases2"], "contact": [{"email": "mailto:jon@urry.me"}, {"github": "github.com/jonurry"}, {"linkedin": "linkedin.com/in/jonurry"}, {"mobile": "tel:+44-7986-371-299"}, {"skype": "skype:jonurry"}, {"twitter2": "twitter.com/jonurry2"}]}'; | |
let jsonObj = JSON.parse(json); | |
let jsonObjCopy = JSON.parse(JSON.stringify(jsonObj)); | |
let jsonObj2 = JSON.parse(json2); | |
console.log(jsonObj); | |
console.log(deepEqual(jsonObj, jsonObj)); | |
// → true | |
console.log(deepEqual(jsonObj, jsonObjCopy)); | |
// → true | |
console.log(deepEqual(jsonObj, jsonObj2)); | |
// → false | |
console.log(deepEqual(null, obj)); | |
// → false | |
console.log(deepEqual(obj, null)); | |
// → false | |
console.log(deepEqual(null, null)); | |
// → true |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I know this comment has a couple years on it, but ....
You can't have a hash with two like key values I'm pretty sure. And knowing JavaScript being all floppy, it doesn't complain about it, it just overwrite's the previously assigned values to that key.