Skip to content

Instantly share code, notes, and snippets.

@litil
Created April 28, 2017 09:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save litil/9885cae53e5a6560d764c568a861123e to your computer and use it in GitHub Desktop.
Save litil/9885cae53e5a6560d764c568a861123e to your computer and use it in GitHub Desktop.
ReactJS - Object Comparison
// from https://medium.com/@nesbtesh/react-best-practices-a76fd0fbef21
export const isObjectEqual = (obj1, obj2) => {
if(!isObject(obj1) || !isObject(obj2)) {
return false;
}
if (obj1 === obj2) {
return true;
}
const item1Keys = Object.keys(obj1).sort();
const item2Keys = Object.keys(obj2).sort();
if (!isArrayEqual(item1Keys, item2Keys)) {
return false;
}
return item2Keys.every(key => {
const value = obj1[key];
const nextValue = obj2[key];
if (value === nextValue) {
return true;
}
return Array.isArray(value) &&
Array.isArray(nextValue) &&
isArrayEqual(value, nextValue);
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment