Skip to content

compare

Signature

js
compare (obj1, obj2, options = {})

Description

Deeply compares two objects or arrays after normalizing them. Returns whether they are equal and a detailed diff of their differences. Differences are reported using dot notation for object keys (a.b.c) and bracket notation for array indices ([0], [1][2].key).

Parameters

NameTypeRequiredDescription
obj1object | arrayyesThe reference object or array
obj2object | arrayyesThe object or array to compare against
optionsobjectnoNormalization options passed to object.normalize
options.ignoredKeysstring[]noKeys to ignore during comparison
options.ignoreCasebooleannoIgnore case on string values
options.ignoreDiacriticsbooleannoIgnore diacritics on string values
options.ignoreSpacesbooleannoNormalize whitespace on string values

Returns

TypeDescription
objectResult object with isEqual and differences
result.isEqualboolean — whether the two inputs are equal after normalization
result.differences.missingstring[] — paths present in obj1 but not in obj2
result.differences.extrastring[] — paths present in obj2 but not in obj1
result.differences.updated{ path, oldValue, newValue }[] — paths with different values

Throws

Throws a TypeError if obj1 or obj2 is not a plain object or array.

Examples

js
// Simple object comparison
compare({ a: 1, b: 2 }, { a: 1, b: 3 })
// {
//   isEqual: false,
//   differences: {
//     missing: [],
//     extra: [],
//     updated: [{ path: 'b', oldValue: 2, newValue: 3 }]
//   }
// }
js
// Key order does not matter
compare({ b: 2, a: 1 }, { a: 1, b: 2 })
// { isEqual: true, differences: { missing: [], extra: [], updated: [] } }
js
// Nested differences
compare({ a: { b: 1, c: 2 } }, { a: { b: 1 } })
// {
//   isEqual: false,
//   differences: {
//     missing: ['a.c'],
//     extra: [],
//     updated: []
//   }
// }
js
// Array comparison
compare([{ a: 1 }, { b: 2 }], [{ a: 99 }, { b: 2 }, { c: 3 }])
// {
//   isEqual: false,
//   differences: {
//     missing: [],
//     extra: ['[2]'],
//     updated: [{ path: '[0].a', oldValue: 1, newValue: 99 }]
//   }
// }
js
// With normalization options
compare({ a: 'Héllo' }, { a: 'hello' }, { ignoreCase: true, ignoreDiacritics: true })
// { isEqual: true, differences: { missing: [], extra: [], updated: [] } }
js
// Ignored keys
compare({ a: 1, b: 'foo' }, { a: 1, b: 'bar' }, { ignoredKeys: ['b'] })
// { isEqual: true, differences: { missing: [], extra: [], updated: [] } }