Last active
July 5, 2017 15:54
-
-
Save Woodsphreaker/186865923374eb3482087a8f80f29e51 to your computer and use it in GitHub Desktop.
Compare Objects
This file contains hidden or 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 a = { | |
k1: 1, | |
k2: 2, | |
k3: { | |
k3_1: 3 | |
} | |
} | |
const b = { | |
k1: 1, | |
k2: 2, | |
k6: { | |
k3_1: 3 | |
} | |
} | |
const isObject = (data) => typeof (data) === "object" | |
const hasOwnProperty = (obj1, obj2, key) => obj1.hasOwnProperty(key) === obj2.hasOwnProperty(key) | |
const compare = (obj1, obj2) => { | |
return Object.keys(obj1).every(key => { | |
if (!hasOwnProperty(obj1, obj2, key)) return false | |
return isObject(obj1[key]) | |
? compare(obj1[key], obj2[key]) | |
: obj1[key] === obj2[key] | |
}) | |
} | |
console.log(compare(a, b)) //false |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment