https://www.npmjs.com/package/deep-equal
function deepEqual(x, y) {
if (x === y) return true;
if (typeof x == "object" && x != null && typeof y == "object" && y != null) {
if (Object.keys(x).length != Object.keys(y).length) return false;
// 比较对象内部
for (var prop in x) {
if (y.hasOwnProperty(prop)) {
if (!deepEqual(x[prop], y[prop])) return false;
}
return false;
}
return true;
}
return false;
}
isEqual
https://gist.github.com/ryangoree/d077e28ff5aee6643c7d1dbaf39bd3b4
const isEqual = (item1, item2) => {
// are they different types?
if (typeof item1 !== typeof item2) {
return false;
}
// if they aren't objects, are their values equal?
if (typeof item1 !== 'object') {
return item1 === item2;
}
// ok, they're objects; do they have different key lengths?
if (Object.keys(item1).length !== Object.keys(item2).length) {
return false;
}
for (let prop in item1) {
if (item1.propertyIsEnumerable(prop)) {
// are any of the property values unequal?
if(!isEqual(item1[prop], item2[prop])) {
return false;
};
}
}
// everything must be equal.
return true;
}