I have two nested objects obj1
and obj2
and I want to compare them and the recursively return an object that for each nested key has a equality-like boolean flag
So for a given obj1
like
obj1 = { prop1: 1, prop2: "foo", prop3: { prop4: 2, prop5: "bar" } }
and the obj2
like
obj2 = { prop1: 3, prop2: "foo", prop3: { prop4: 2, prop5: "foobar" }, prop6: "new" }
it should return
equality = { prop1: false, prop2: true, prop3 : { prop4: true, prop5: false }, prop6: false }
If an object has a new property, like obj2.prop6
, then the equality will be equality.prop6 = false
.
For non-nested object a simple keys comparison solutions is here Get the property of the difference between two objects in javascript While to recursively compare nested objects it is showed here JavaScript: Deep comparison recursively: Objects and properties
Advertisement
Answer
You could use reduce
to build new object and another get
method to get nested props from other object by string
and compare it to current prop value in first object.
const obj1 = { prop1: 1, prop2: "foo", prop3: { prop4: 2, prop5: "bar" } } const obj2 = { prop1: 3, prop2: "foo", prop3: { prop4: 2, prop5: "foobar" } } function get(obj, path) { return path.split('.').reduce((r, e) => { if (!r) return r else return r[e] || undefined }, obj) } function compare(a, b, prev = "") { return Object.keys(a).reduce((r, e) => { const path = prev + (prev ? '.' + e : e); const value = a[e] === get(b, path); r[e] = typeof a[e] === 'object' ? compare(a[e], b, path) : value return r; }, {}) } const result = compare(obj1, obj2); console.log(result)
To compare all properties of both objects you could create extra function that will perform loop by both objects.
const obj1 = {"prop1":1,"prop2":"foo","prop3":{"prop4":2,"prop5":"bar"},"prop7":{"prop9":{"prop10":"foo"}}} const obj2 = {"prop1":3,"prop2":"foo","prop3":{"prop4":2,"prop5":"foobar"},"prop6":"new","prop7":{"foo":"foo","bar":{"baz":"baz"}}} function get(obj, path) { return path.split('.').reduce((r, e) => { if (!r) return r; else return r[e] || undefined; }, obj); } function isEmpty(o) { if (typeof o !== 'object') return true; else return !Object.keys(o).length; } function build(a, b, o = null, prev = '') { return Object.keys(a).reduce( (r, e) => { const path = prev + (prev ? '.' + e : e); const bObj = get(b, path); const value = a[e] === bObj; if (typeof a[e] === 'object') { if (isEmpty(a[e]) && isEmpty(bObj)) { if (e in r) r[e] = r[e]; else r[e] = true; } else if (!bObj && isEmpty(a[e])) { r[e] = value; } else { r[e] = build(a[e], b, r[e], path); } } else { r[e] = value; } return r; }, o ? o : {} ); } function compare(a, b) { const o = build(a, b); return build(b, a, o); } const result = compare(obj1, obj2); console.log(result)