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 |
if (a === b) {
// items are identical
return true;
}
you can add this line
if (a === b) {
// items are identical
return true && Object.is(a,b);
}
in this way it will not evaluate as true the following expression:
deepEqual(+0, -0); // true
Hey, I found a bug in the code.
deepEqual({here: "is", here: "is"}, {here: "is"}) // returns true
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.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi I came up with the following solution but it is not working for second and third tests as expected, any insights would be appreciated:
// Your code here.
function deepEqual(val1, val2){
if ((val1 != null && typeof val1 == "object") && (val2 != null && typeof val2 == "object")){
} else {
return val1 === val2;
};
};
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