Skip to content

Instantly share code, notes, and snippets.

@alexreardon
Created April 12, 2016 10:38
Show Gist options
  • Save alexreardon/b14f138c07c119555e412753fd4979a4 to your computer and use it in GitHub Desktop.
Save alexreardon/b14f138c07c119555e412753fd4979a4 to your computer and use it in GitHub Desktop.
const isObjectEqual = (obj1, obj2) => {
if(!isObject(obj1) || !isObject(obj2)) {
return false;
}
// are the references the same?
if (obj1 === obj2) {
return true;
}
// does it contain objects with the same keys?
const item1Keys = Object.keys(obj1).sort();
const item2Keys = Object.keys(obj2).sort();
if (!isArrayEqual(item1Keys, item2Keys)) {
return false;
}
// does every object in props have the same reference?
return item2Keys.every(key => {
const value = obj1[key];
const nextValue = obj2[key];
if (value === nextValue) {
return true;
}
// special case for arrays - check one level deep
return Array.isArray(value) &&
Array.isArray(nextValue) &&
isArrayEqual(value, nextValue);
});
};
const isArrayEqual = (array1 = [], array2 = []) => {
if (array1 === array2) {
return true;
}
// check one level deep
return array1.length === array2.length &&
array1.every((item, index) => item === array2[index]);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment