Skip to content
Advertisement

Compare nested objects in JavaScript and return keys equality

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

JavaScript

and the obj2 like

JavaScript

it should return

JavaScript

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.

JavaScript

To compare all properties of both objects you could create extra function that will perform loop by both objects.

JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement