Created
August 1, 2020 23:35
-
-
Save itsalb3rt/485c965a8d983dcb73a44d7b4afd5280 to your computer and use it in GitHub Desktop.
Object deep comparation
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
/** | |
* Deep object comparation | |
* | |
* https://dmitripavlutin.com/how-to-compare-objects-in-javascript/ | |
*/ | |
export default { | |
methods: { | |
deepEqual (object1, object2) { | |
const keys1 = Object.keys(object1) | |
const keys2 = Object.keys(object2) | |
if (keys1.length !== keys2.length) { | |
return false | |
} | |
for (const key of keys1) { | |
const val1 = object1[key] | |
const val2 = object2[key] | |
const areObjects = this.isObject(val1) && this.isObject(val2) | |
if ( | |
// eslint-disable-next-line no-mixed-operators | |
areObjects && !this.deepEqual(val1, val2) || !areObjects && val1 !== val2 | |
) { | |
return false | |
} | |
} | |
return true | |
}, | |
isObject (object) { | |
return object != null && typeof object === 'object' | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment