Skip to content

Instantly share code, notes, and snippets.

@Woodsphreaker
Last active July 5, 2017 15:54
Show Gist options
  • Save Woodsphreaker/186865923374eb3482087a8f80f29e51 to your computer and use it in GitHub Desktop.
Save Woodsphreaker/186865923374eb3482087a8f80f29e51 to your computer and use it in GitHub Desktop.
Compare Objects
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