Skip to content

Instantly share code, notes, and snippets.

@jrmarqueshd
Last active March 19, 2020 12:07
Show Gist options
  • Save jrmarqueshd/a05327bace5fb477eaec845fa0a57142 to your computer and use it in GitHub Desktop.
Save jrmarqueshd/a05327bace5fb477eaec845fa0a57142 to your computer and use it in GitHub Desktop.
Compare if two objects is the same
isEquivalent = (a, b) => {
// Create arrays of property names
var aProps = Object.getOwnPropertyNames(a);
var bProps = Object.getOwnPropertyNames(b);
// If number of properties is different,
// objects are not equivalent
if (aProps.length != bProps.length) {
return false;
}
for (var i = 0; i < aProps.length; i++) {
var propName = aProps[i];
// If values of same property are not equal,
// objects are not equivalent
if (a[propName] !== b[propName]) {
return false;
}
}
// If we made it this far, objects
// are considered equivalent
return true;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment