Deep compare two objects to check if they are equal
npm install object-equals
yarn add object-equals
import { objectEquals } from 'object-equals'
// Or
// import objectEquals from 'object-equals'
// Or
// var { objectEquals } = require('object-equals')
const obj1 = {
a: 1,
c: {
a: 11,
d: {}
}
}
const obj2 = {
a: 1,
c: {
a: 11,
d: {}
}
}
expect(objectEquals(obj1, obj2)).toEqual(true)
- Level of deep comparison, after which no deep comparison is done
const obj1 = {
a: 1,
b: {
c: '2'
}
}
const obj2 = {
a: 1,
b: {
c: '3'
}
}
expect(objectEquals(obj1, obj2)).toEqual(false)
expect(objectEquals(obj1, obj2, {depth: 1})).toEqual(true)
expect(objectEquals(obj1, obj2, {depth: 2})).toEqual(false)
- if true, only keys will be compared irrespective of the values
const obj1 = {
a: 1,
b: {
c: '2'
}
}
const obj2 = {
a: 1,
b: {
c: '3'
}
}
expect(objectEquals(obj1, obj2)).toEqual(false)
expect(objectEquals(obj1, obj2, {keysOnly: true})).toEqual(true)
- if true, only values will be compared irrespective of the keys
const obj1 = {
a: 1,
b: {
c: '2'
}
}
const obj2 = {
aaa: 1,
bbbb: {
cccc: '2'
}
}
expect(objectEquals(obj1, obj2)).toEqual(false)
expect(objectEquals(obj1, obj2, {valuesOnly: true})).toEqual(true)