30秒学会 JavaScript 片段 · 2022年7月18日

30秒学会 JavaScript 片段 – How do I compare two objects in JavaScript?

Equality comparison

Even though two different objects can have the same properties with equal values, they are not considered equal when compared using either the loose or strict equality operators (== or ===). This is because arrays and objects in JavaScript are compared by reference. This is unlike primitive values which are compared by value.

代码实现

const a = { name: 'John', age: 26 };
const b = { name: 'John', age: 26 };

a === b; // false

JSON.stringify

JSON.stringify() often comes up as the solution to this problem. While it can be useful in some situations, comparing the serialized strings can have its own pitfalls. The most common of these has to do with similar, but not equal, values that result in the same serialized string.

使用样例

const equals = (a, b) => JSON.stringify(a) === JSON.stringify(b);

const a = { name: 'John', age: 26 };
const b = { name: 'John', age: 26 };

equals(a, b); // true

const c = { name: 'John' };
const d = { name: 'John', age: undefined };

equals(c, d); // true, should be false

Deep equality comparison

As it turns out, comparing two objects is not trivial. This is the reason why shallow or deep equality comparison helper functions are so common. These usually use recursion to deeply compare two objects, accounting for most scenarios such as empty values, special types and nesting.

const equals = (a, b) => {
  if (a === b) return true;
  if (a instanceof Date && b instanceof Date)
    return a.getTime() === b.getTime();
  if (!a || !b || (typeof a !== 'object' && typeof b !== 'object'))
    return a === b;
  if (a.prototype !== b.prototype) return false;
  const keys = Object.keys(a);
  if (keys.length !== Object.keys(b).length) return false;
  return keys.every(k => equals(a[k], b[k]));
};

const a = { name: 'John', age: 26 };
const b = { name: 'John', age: 26 };

equals(a, b); // true

const c = { name: 'John' };
const d = { name: 'John', age: undefined };

equals(c, d); // false

The above helper function handles all of these issues and is explained in more depth in the equals snippet.

翻译自:https://www.30secondsofcode.org/js/s/object-comparison