https://www.npmjs.com/package/deep-equal

  1. function deepEqual(x, y) {
  2. if (x === y) return true;
  3. if (typeof x == "object" && x != null && typeof y == "object" && y != null) {
  4. if (Object.keys(x).length != Object.keys(y).length) return false;
  5. // 比较对象内部
  6. for (var prop in x) {
  7. if (y.hasOwnProperty(prop)) {
  8. if (!deepEqual(x[prop], y[prop])) return false;
  9. }
  10. return false;
  11. }
  12. return true;
  13. }
  14. return false;
  15. }

isEqual

https://gist.github.com/ryangoree/d077e28ff5aee6643c7d1dbaf39bd3b4

  1. const isEqual = (item1, item2) => {
  2. // are they different types?
  3. if (typeof item1 !== typeof item2) {
  4. return false;
  5. }
  6. // if they aren't objects, are their values equal?
  7. if (typeof item1 !== 'object') {
  8. return item1 === item2;
  9. }
  10. // ok, they're objects; do they have different key lengths?
  11. if (Object.keys(item1).length !== Object.keys(item2).length) {
  12. return false;
  13. }
  14. for (let prop in item1) {
  15. if (item1.propertyIsEnumerable(prop)) {
  16. // are any of the property values unequal?
  17. if(!isEqual(item1[prop], item2[prop])) {
  18. return false;
  19. };
  20. }
  21. }
  22. // everything must be equal.
  23. return true;
  24. }