Compare nested objects in JavaScript and return keys equality The 2019 Stack Overflow Developer Survey Results Are InJavaScript: Deep comparison recursively: Objects and propertiesGet the property of the difference between two objects in javascriptLength of a JavaScript objectWhat is the most efficient way to deep clone an object in JavaScript?How do I remove a property from a JavaScript object?How do I check if an array includes an object in JavaScript?Which equals operator (== vs ===) should be used in JavaScript comparisons?Compare two dates with JavaScriptHow do I test for an empty JavaScript object?How do I loop through or enumerate a JavaScript object?How do I correctly clone a JavaScript object?Checking if a key exists in a JavaScript object?

Any good smartcontract for "business calendar" oracles?

If a poisoned arrow's piercing damage is reduced to 0, do you still get poisoned?

Are there any other methods to apply to solving simultaneous equations?

"Riffle" two strings

Why could you hear an Amstrad CPC working?

Idiomatic way to prevent slicing?

Where does the "burst of radiance" from Holy Weapon originate?

Monty Hall variation

The difference between dialogue marks

How to manage monthly salary

A poker game description that does not feel gimmicky

Should I write numbers in words or as symbols in this case?

How to answer pointed "are you quitting" questioning when I don't want them to suspect

Springs with some finite mass

Is flight data recorder erased after every flight?

Why can Shazam do this?

How to make payment on the internet without leaving a money trail?

Does light intensity oscillate really fast since it is a wave?

What does "sndry explns" mean in one of the Hitchhiker's guide books?

Is "plugging out" electronic devices an American expression?

Why is the maximum length of openwrt’s root password 8 characters?

I looked up a future colleague on linkedin before I started a job. I told my colleague about it and he seemed surprised. Should I apologize?

Patience, young "Padovan"

Why don't Unix/Linux systems traverse through directories until they find the required version of a linked library?



Compare nested objects in JavaScript and return keys equality



The 2019 Stack Overflow Developer Survey Results Are InJavaScript: Deep comparison recursively: Objects and propertiesGet the property of the difference between two objects in javascriptLength of a JavaScript objectWhat is the most efficient way to deep clone an object in JavaScript?How do I remove a property from a JavaScript object?How do I check if an array includes an object in JavaScript?Which equals operator (== vs ===) should be used in JavaScript comparisons?Compare two dates with JavaScriptHow do I test for an empty JavaScript object?How do I loop through or enumerate a JavaScript object?How do I correctly clone a JavaScript object?Checking if a key exists in a JavaScript object?



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








7















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










share|improve this question



















  • 1





    Will both objects always have exact match properties?

    – holydragon
    12 hours ago











  • good point. Nope, so the equality could have a new key set to false. Updating with this point. Thank you.

    – loretoparisi
    12 hours ago


















7















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










share|improve this question



















  • 1





    Will both objects always have exact match properties?

    – holydragon
    12 hours ago











  • good point. Nope, so the equality could have a new key set to false. Updating with this point. Thank you.

    – loretoparisi
    12 hours ago














7












7








7


1






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










share|improve this question
















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







javascript






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 12 hours ago







loretoparisi

















asked 12 hours ago









loretoparisiloretoparisi

8,07154973




8,07154973







  • 1





    Will both objects always have exact match properties?

    – holydragon
    12 hours ago











  • good point. Nope, so the equality could have a new key set to false. Updating with this point. Thank you.

    – loretoparisi
    12 hours ago













  • 1





    Will both objects always have exact match properties?

    – holydragon
    12 hours ago











  • good point. Nope, so the equality could have a new key set to false. Updating with this point. Thank you.

    – loretoparisi
    12 hours ago








1




1





Will both objects always have exact match properties?

– holydragon
12 hours ago





Will both objects always have exact match properties?

– holydragon
12 hours ago













good point. Nope, so the equality could have a new key set to false. Updating with this point. Thank you.

– loretoparisi
12 hours ago






good point. Nope, so the equality could have a new key set to false. Updating with this point. Thank you.

– loretoparisi
12 hours ago













4 Answers
4






active

oldest

votes


















1














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] , 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] , 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)








share|improve this answer




















  • 1





    I thinks this is the best solution since it supports all recent version of ECMAScript.

    – loretoparisi
    11 hours ago











  • Just a thing, supposed that the property is a void object like , it does not make the comparison.

    – loretoparisi
    11 hours ago






  • 1





    In that case you could use this jsfiddle.net/vqwn3zLf

    – Nenad Vracar
    11 hours ago











  • Ok, now it handles a nested lonely prop11= object, but if you have like the same prop12= in both, you will get the as result for the prop12 keyword, instead of bool. See here jsfiddle.net/gpu20nwy

    – loretoparisi
    10 hours ago







  • 1





    @loretoparisi Try this jsfiddle.net/r0y8nd3q/1

    – Nenad Vracar
    10 hours ago


















5














You could iterate all keys and check the nested objects if both values are objects.






const isObject = v => v && typeof v === 'object';

function getDifference(a, b)
return Object.assign(...Array.from(
new Set([...Object.keys(a), ...Object.keys(b)]),
k => ( [k]: isObject(a[k]) && isObject(b[k])
? getDifference(a[k], b[k])
: a[k] === b[k]
)
));


var obj1 = prop1: 1, prop2: "foo", prop3: prop4: 2, prop5: "bar" ,
obj2 = prop1: 3, prop2: "foo", prop3: prop4: 2, prop5: "foobar" , prop6: "new" ;

console.log(getDifference(obj1, obj2));








share|improve this answer

























  • Thanks! This solution works in every condition even if the property is a void object like prop7 = . Maybe avoiding the ECMA6 Spread notation for more compatibility could help.

    – loretoparisi
    11 hours ago







  • 1





    without spread not Set.

    – Nina Scholz
    11 hours ago











  • maybe like Object.assign(Array.from(new Set( [].concat(Object.keys(a)).concat(Object.keys(b)))

    – loretoparisi
    11 hours ago






  • 1





    w h a t e v e r ...

    – Nina Scholz
    11 hours ago


















2














Loop through each key and compare the properties. If the property is an object, recursively compare the properties. This will work for any level of nesting. Since the properties could be missing from either of the objects value || check is added.






const obj1=prop1:1,prop2:"foo",prop3:prop4:2,prop5:"bar",prop7:pro8:"only in 1",
obj2=prop1:3,prop2:"foo",prop3:prop4:2,prop5:"foobar", prop6: "only in 2";

const isObject = val => val && typeof val === 'object'; // required for "null" comparison

function compare(obj1, obj2)
let equality = ,
merged = ...obj1, ...obj2 ; // has properties of both

for (let key in merged)

return equality;


console.log(compare(obj1, obj2))








share|improve this answer

























  • Thanks, just updated the code, assuming you can have new properties as well.

    – loretoparisi
    12 hours ago











  • what if obj1 have some key with a child obj as its value and obj2 does not have the same key, then the method will be called with compare(obj1, undefined) which will throw an error at obj2[key]

    – AZ_
    12 hours ago











  • @loretoparisi updated

    – adiga
    12 hours ago











  • @adiga not sure, but the comments in the question do mention that, and the updated question as well, it cant be assumed that extra ket always have a string value.

    – AZ_
    12 hours ago












  • @AZ_ updated. Not sure if it will fail for any scenario

    – adiga
    12 hours ago


















1














A recursive example,






var obj1 = 
prop1: 1,
prop2: "foo",
prop3:
prop4: 2,
prop5: "bar"
,
prop7: ,


var obj2 =
prop1: 3,
prop2: "foo",
prop3:
prop4: 2,
prop5: "foobar"
,
prop6: "new",
prop7: ,
prop8: ,


var result = ;

function compare(obj1, obj2, obj_)
for (let k in obj1)
var type = typeof obj1[k];
if (type === 'object')
obj_[k] = ;
if (!obj2[k])
obj_[k] = false;
else if ((Object.entries(obj1[k]).length === 0 && obj1[k].constructor === Object) && (Object.entries(obj2[k]).length === 0 && obj2[k].constructor === Object))
obj_[k] = true;
else
compare(obj1[k], obj2[k], obj_[k]);

else
obj_[k] = (obj1[k] === obj2[k]);





if (Object.keys(obj1).length < Object.keys(obj2).length) //check if both objects vary in length.
var tmp = obj1;
obj1 = obj2;
obj2 = tmp;


compare(obj1, obj2, result);

console.log(result);








share|improve this answer

























  • Thank you. If you try this case - jsfiddle.net/47fcqtom you will get a in the results for properties of type object that belong to both objects and that are .

    – loretoparisi
    10 hours ago







  • 1





    @loretoparisi I made a change, try now.

    – Shoyeb Sheikh
    10 hours ago











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55591096%2fcompare-nested-objects-in-javascript-and-return-keys-equality%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























4 Answers
4






active

oldest

votes








4 Answers
4






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














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] , 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] , 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)








share|improve this answer




















  • 1





    I thinks this is the best solution since it supports all recent version of ECMAScript.

    – loretoparisi
    11 hours ago











  • Just a thing, supposed that the property is a void object like , it does not make the comparison.

    – loretoparisi
    11 hours ago






  • 1





    In that case you could use this jsfiddle.net/vqwn3zLf

    – Nenad Vracar
    11 hours ago











  • Ok, now it handles a nested lonely prop11= object, but if you have like the same prop12= in both, you will get the as result for the prop12 keyword, instead of bool. See here jsfiddle.net/gpu20nwy

    – loretoparisi
    10 hours ago







  • 1





    @loretoparisi Try this jsfiddle.net/r0y8nd3q/1

    – Nenad Vracar
    10 hours ago















1














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] , 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] , 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)








share|improve this answer




















  • 1





    I thinks this is the best solution since it supports all recent version of ECMAScript.

    – loretoparisi
    11 hours ago











  • Just a thing, supposed that the property is a void object like , it does not make the comparison.

    – loretoparisi
    11 hours ago






  • 1





    In that case you could use this jsfiddle.net/vqwn3zLf

    – Nenad Vracar
    11 hours ago











  • Ok, now it handles a nested lonely prop11= object, but if you have like the same prop12= in both, you will get the as result for the prop12 keyword, instead of bool. See here jsfiddle.net/gpu20nwy

    – loretoparisi
    10 hours ago







  • 1





    @loretoparisi Try this jsfiddle.net/r0y8nd3q/1

    – Nenad Vracar
    10 hours ago













1












1








1







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] , 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] , 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)








share|improve this 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] , 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] , 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)








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] , 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)





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] , 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)





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] , 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)





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] , 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)






share|improve this answer














share|improve this answer



share|improve this answer








edited 10 hours ago

























answered 12 hours ago









Nenad VracarNenad Vracar

73.3k126085




73.3k126085







  • 1





    I thinks this is the best solution since it supports all recent version of ECMAScript.

    – loretoparisi
    11 hours ago











  • Just a thing, supposed that the property is a void object like , it does not make the comparison.

    – loretoparisi
    11 hours ago






  • 1





    In that case you could use this jsfiddle.net/vqwn3zLf

    – Nenad Vracar
    11 hours ago











  • Ok, now it handles a nested lonely prop11= object, but if you have like the same prop12= in both, you will get the as result for the prop12 keyword, instead of bool. See here jsfiddle.net/gpu20nwy

    – loretoparisi
    10 hours ago







  • 1





    @loretoparisi Try this jsfiddle.net/r0y8nd3q/1

    – Nenad Vracar
    10 hours ago












  • 1





    I thinks this is the best solution since it supports all recent version of ECMAScript.

    – loretoparisi
    11 hours ago











  • Just a thing, supposed that the property is a void object like , it does not make the comparison.

    – loretoparisi
    11 hours ago






  • 1





    In that case you could use this jsfiddle.net/vqwn3zLf

    – Nenad Vracar
    11 hours ago











  • Ok, now it handles a nested lonely prop11= object, but if you have like the same prop12= in both, you will get the as result for the prop12 keyword, instead of bool. See here jsfiddle.net/gpu20nwy

    – loretoparisi
    10 hours ago







  • 1





    @loretoparisi Try this jsfiddle.net/r0y8nd3q/1

    – Nenad Vracar
    10 hours ago







1




1





I thinks this is the best solution since it supports all recent version of ECMAScript.

– loretoparisi
11 hours ago





I thinks this is the best solution since it supports all recent version of ECMAScript.

– loretoparisi
11 hours ago













Just a thing, supposed that the property is a void object like , it does not make the comparison.

– loretoparisi
11 hours ago





Just a thing, supposed that the property is a void object like , it does not make the comparison.

– loretoparisi
11 hours ago




1




1





In that case you could use this jsfiddle.net/vqwn3zLf

– Nenad Vracar
11 hours ago





In that case you could use this jsfiddle.net/vqwn3zLf

– Nenad Vracar
11 hours ago













Ok, now it handles a nested lonely prop11= object, but if you have like the same prop12= in both, you will get the as result for the prop12 keyword, instead of bool. See here jsfiddle.net/gpu20nwy

– loretoparisi
10 hours ago






Ok, now it handles a nested lonely prop11= object, but if you have like the same prop12= in both, you will get the as result for the prop12 keyword, instead of bool. See here jsfiddle.net/gpu20nwy

– loretoparisi
10 hours ago





1




1





@loretoparisi Try this jsfiddle.net/r0y8nd3q/1

– Nenad Vracar
10 hours ago





@loretoparisi Try this jsfiddle.net/r0y8nd3q/1

– Nenad Vracar
10 hours ago













5














You could iterate all keys and check the nested objects if both values are objects.






const isObject = v => v && typeof v === 'object';

function getDifference(a, b)
return Object.assign(...Array.from(
new Set([...Object.keys(a), ...Object.keys(b)]),
k => ( [k]: isObject(a[k]) && isObject(b[k])
? getDifference(a[k], b[k])
: a[k] === b[k]
)
));


var obj1 = prop1: 1, prop2: "foo", prop3: prop4: 2, prop5: "bar" ,
obj2 = prop1: 3, prop2: "foo", prop3: prop4: 2, prop5: "foobar" , prop6: "new" ;

console.log(getDifference(obj1, obj2));








share|improve this answer

























  • Thanks! This solution works in every condition even if the property is a void object like prop7 = . Maybe avoiding the ECMA6 Spread notation for more compatibility could help.

    – loretoparisi
    11 hours ago







  • 1





    without spread not Set.

    – Nina Scholz
    11 hours ago











  • maybe like Object.assign(Array.from(new Set( [].concat(Object.keys(a)).concat(Object.keys(b)))

    – loretoparisi
    11 hours ago






  • 1





    w h a t e v e r ...

    – Nina Scholz
    11 hours ago















5














You could iterate all keys and check the nested objects if both values are objects.






const isObject = v => v && typeof v === 'object';

function getDifference(a, b)
return Object.assign(...Array.from(
new Set([...Object.keys(a), ...Object.keys(b)]),
k => ( [k]: isObject(a[k]) && isObject(b[k])
? getDifference(a[k], b[k])
: a[k] === b[k]
)
));


var obj1 = prop1: 1, prop2: "foo", prop3: prop4: 2, prop5: "bar" ,
obj2 = prop1: 3, prop2: "foo", prop3: prop4: 2, prop5: "foobar" , prop6: "new" ;

console.log(getDifference(obj1, obj2));








share|improve this answer

























  • Thanks! This solution works in every condition even if the property is a void object like prop7 = . Maybe avoiding the ECMA6 Spread notation for more compatibility could help.

    – loretoparisi
    11 hours ago







  • 1





    without spread not Set.

    – Nina Scholz
    11 hours ago











  • maybe like Object.assign(Array.from(new Set( [].concat(Object.keys(a)).concat(Object.keys(b)))

    – loretoparisi
    11 hours ago






  • 1





    w h a t e v e r ...

    – Nina Scholz
    11 hours ago













5












5








5







You could iterate all keys and check the nested objects if both values are objects.






const isObject = v => v && typeof v === 'object';

function getDifference(a, b)
return Object.assign(...Array.from(
new Set([...Object.keys(a), ...Object.keys(b)]),
k => ( [k]: isObject(a[k]) && isObject(b[k])
? getDifference(a[k], b[k])
: a[k] === b[k]
)
));


var obj1 = prop1: 1, prop2: "foo", prop3: prop4: 2, prop5: "bar" ,
obj2 = prop1: 3, prop2: "foo", prop3: prop4: 2, prop5: "foobar" , prop6: "new" ;

console.log(getDifference(obj1, obj2));








share|improve this answer















You could iterate all keys and check the nested objects if both values are objects.






const isObject = v => v && typeof v === 'object';

function getDifference(a, b)
return Object.assign(...Array.from(
new Set([...Object.keys(a), ...Object.keys(b)]),
k => ( [k]: isObject(a[k]) && isObject(b[k])
? getDifference(a[k], b[k])
: a[k] === b[k]
)
));


var obj1 = prop1: 1, prop2: "foo", prop3: prop4: 2, prop5: "bar" ,
obj2 = prop1: 3, prop2: "foo", prop3: prop4: 2, prop5: "foobar" , prop6: "new" ;

console.log(getDifference(obj1, obj2));








const isObject = v => v && typeof v === 'object';

function getDifference(a, b)
return Object.assign(...Array.from(
new Set([...Object.keys(a), ...Object.keys(b)]),
k => ( [k]: isObject(a[k]) && isObject(b[k])
? getDifference(a[k], b[k])
: a[k] === b[k]
)
));


var obj1 = prop1: 1, prop2: "foo", prop3: prop4: 2, prop5: "bar" ,
obj2 = prop1: 3, prop2: "foo", prop3: prop4: 2, prop5: "foobar" , prop6: "new" ;

console.log(getDifference(obj1, obj2));





const isObject = v => v && typeof v === 'object';

function getDifference(a, b)
return Object.assign(...Array.from(
new Set([...Object.keys(a), ...Object.keys(b)]),
k => ( [k]: isObject(a[k]) && isObject(b[k])
? getDifference(a[k], b[k])
: a[k] === b[k]
)
));


var obj1 = prop1: 1, prop2: "foo", prop3: prop4: 2, prop5: "bar" ,
obj2 = prop1: 3, prop2: "foo", prop3: prop4: 2, prop5: "foobar" , prop6: "new" ;

console.log(getDifference(obj1, obj2));






share|improve this answer














share|improve this answer



share|improve this answer








edited 12 hours ago

























answered 12 hours ago









Nina ScholzNina Scholz

197k15109179




197k15109179












  • Thanks! This solution works in every condition even if the property is a void object like prop7 = . Maybe avoiding the ECMA6 Spread notation for more compatibility could help.

    – loretoparisi
    11 hours ago







  • 1





    without spread not Set.

    – Nina Scholz
    11 hours ago











  • maybe like Object.assign(Array.from(new Set( [].concat(Object.keys(a)).concat(Object.keys(b)))

    – loretoparisi
    11 hours ago






  • 1





    w h a t e v e r ...

    – Nina Scholz
    11 hours ago

















  • Thanks! This solution works in every condition even if the property is a void object like prop7 = . Maybe avoiding the ECMA6 Spread notation for more compatibility could help.

    – loretoparisi
    11 hours ago







  • 1





    without spread not Set.

    – Nina Scholz
    11 hours ago











  • maybe like Object.assign(Array.from(new Set( [].concat(Object.keys(a)).concat(Object.keys(b)))

    – loretoparisi
    11 hours ago






  • 1





    w h a t e v e r ...

    – Nina Scholz
    11 hours ago
















Thanks! This solution works in every condition even if the property is a void object like prop7 = . Maybe avoiding the ECMA6 Spread notation for more compatibility could help.

– loretoparisi
11 hours ago






Thanks! This solution works in every condition even if the property is a void object like prop7 = . Maybe avoiding the ECMA6 Spread notation for more compatibility could help.

– loretoparisi
11 hours ago





1




1





without spread not Set.

– Nina Scholz
11 hours ago





without spread not Set.

– Nina Scholz
11 hours ago













maybe like Object.assign(Array.from(new Set( [].concat(Object.keys(a)).concat(Object.keys(b)))

– loretoparisi
11 hours ago





maybe like Object.assign(Array.from(new Set( [].concat(Object.keys(a)).concat(Object.keys(b)))

– loretoparisi
11 hours ago




1




1





w h a t e v e r ...

– Nina Scholz
11 hours ago





w h a t e v e r ...

– Nina Scholz
11 hours ago











2














Loop through each key and compare the properties. If the property is an object, recursively compare the properties. This will work for any level of nesting. Since the properties could be missing from either of the objects value || check is added.






const obj1=prop1:1,prop2:"foo",prop3:prop4:2,prop5:"bar",prop7:pro8:"only in 1",
obj2=prop1:3,prop2:"foo",prop3:prop4:2,prop5:"foobar", prop6: "only in 2";

const isObject = val => val && typeof val === 'object'; // required for "null" comparison

function compare(obj1, obj2)
let equality = ,
merged = ...obj1, ...obj2 ; // has properties of both

for (let key in merged)

return equality;


console.log(compare(obj1, obj2))








share|improve this answer

























  • Thanks, just updated the code, assuming you can have new properties as well.

    – loretoparisi
    12 hours ago











  • what if obj1 have some key with a child obj as its value and obj2 does not have the same key, then the method will be called with compare(obj1, undefined) which will throw an error at obj2[key]

    – AZ_
    12 hours ago











  • @loretoparisi updated

    – adiga
    12 hours ago











  • @adiga not sure, but the comments in the question do mention that, and the updated question as well, it cant be assumed that extra ket always have a string value.

    – AZ_
    12 hours ago












  • @AZ_ updated. Not sure if it will fail for any scenario

    – adiga
    12 hours ago















2














Loop through each key and compare the properties. If the property is an object, recursively compare the properties. This will work for any level of nesting. Since the properties could be missing from either of the objects value || check is added.






const obj1=prop1:1,prop2:"foo",prop3:prop4:2,prop5:"bar",prop7:pro8:"only in 1",
obj2=prop1:3,prop2:"foo",prop3:prop4:2,prop5:"foobar", prop6: "only in 2";

const isObject = val => val && typeof val === 'object'; // required for "null" comparison

function compare(obj1, obj2)
let equality = ,
merged = ...obj1, ...obj2 ; // has properties of both

for (let key in merged)

return equality;


console.log(compare(obj1, obj2))








share|improve this answer

























  • Thanks, just updated the code, assuming you can have new properties as well.

    – loretoparisi
    12 hours ago











  • what if obj1 have some key with a child obj as its value and obj2 does not have the same key, then the method will be called with compare(obj1, undefined) which will throw an error at obj2[key]

    – AZ_
    12 hours ago











  • @loretoparisi updated

    – adiga
    12 hours ago











  • @adiga not sure, but the comments in the question do mention that, and the updated question as well, it cant be assumed that extra ket always have a string value.

    – AZ_
    12 hours ago












  • @AZ_ updated. Not sure if it will fail for any scenario

    – adiga
    12 hours ago













2












2








2







Loop through each key and compare the properties. If the property is an object, recursively compare the properties. This will work for any level of nesting. Since the properties could be missing from either of the objects value || check is added.






const obj1=prop1:1,prop2:"foo",prop3:prop4:2,prop5:"bar",prop7:pro8:"only in 1",
obj2=prop1:3,prop2:"foo",prop3:prop4:2,prop5:"foobar", prop6: "only in 2";

const isObject = val => val && typeof val === 'object'; // required for "null" comparison

function compare(obj1, obj2)
let equality = ,
merged = ...obj1, ...obj2 ; // has properties of both

for (let key in merged)

return equality;


console.log(compare(obj1, obj2))








share|improve this answer















Loop through each key and compare the properties. If the property is an object, recursively compare the properties. This will work for any level of nesting. Since the properties could be missing from either of the objects value || check is added.






const obj1=prop1:1,prop2:"foo",prop3:prop4:2,prop5:"bar",prop7:pro8:"only in 1",
obj2=prop1:3,prop2:"foo",prop3:prop4:2,prop5:"foobar", prop6: "only in 2";

const isObject = val => val && typeof val === 'object'; // required for "null" comparison

function compare(obj1, obj2)
let equality = ,
merged = ...obj1, ...obj2 ; // has properties of both

for (let key in merged)

return equality;


console.log(compare(obj1, obj2))








const obj1=prop1:1,prop2:"foo",prop3:prop4:2,prop5:"bar",prop7:pro8:"only in 1",
obj2=prop1:3,prop2:"foo",prop3:prop4:2,prop5:"foobar", prop6: "only in 2";

const isObject = val => val && typeof val === 'object'; // required for "null" comparison

function compare(obj1, obj2)
let equality = ,
merged = ...obj1, ...obj2 ; // has properties of both

for (let key in merged)

return equality;


console.log(compare(obj1, obj2))





const obj1=prop1:1,prop2:"foo",prop3:prop4:2,prop5:"bar",prop7:pro8:"only in 1",
obj2=prop1:3,prop2:"foo",prop3:prop4:2,prop5:"foobar", prop6: "only in 2";

const isObject = val => val && typeof val === 'object'; // required for "null" comparison

function compare(obj1, obj2)
let equality = ,
merged = ...obj1, ...obj2 ; // has properties of both

for (let key in merged)

return equality;


console.log(compare(obj1, obj2))






share|improve this answer














share|improve this answer



share|improve this answer








edited 11 hours ago

























answered 12 hours ago









adigaadiga

12.3k62645




12.3k62645












  • Thanks, just updated the code, assuming you can have new properties as well.

    – loretoparisi
    12 hours ago











  • what if obj1 have some key with a child obj as its value and obj2 does not have the same key, then the method will be called with compare(obj1, undefined) which will throw an error at obj2[key]

    – AZ_
    12 hours ago











  • @loretoparisi updated

    – adiga
    12 hours ago











  • @adiga not sure, but the comments in the question do mention that, and the updated question as well, it cant be assumed that extra ket always have a string value.

    – AZ_
    12 hours ago












  • @AZ_ updated. Not sure if it will fail for any scenario

    – adiga
    12 hours ago

















  • Thanks, just updated the code, assuming you can have new properties as well.

    – loretoparisi
    12 hours ago











  • what if obj1 have some key with a child obj as its value and obj2 does not have the same key, then the method will be called with compare(obj1, undefined) which will throw an error at obj2[key]

    – AZ_
    12 hours ago











  • @loretoparisi updated

    – adiga
    12 hours ago











  • @adiga not sure, but the comments in the question do mention that, and the updated question as well, it cant be assumed that extra ket always have a string value.

    – AZ_
    12 hours ago












  • @AZ_ updated. Not sure if it will fail for any scenario

    – adiga
    12 hours ago
















Thanks, just updated the code, assuming you can have new properties as well.

– loretoparisi
12 hours ago





Thanks, just updated the code, assuming you can have new properties as well.

– loretoparisi
12 hours ago













what if obj1 have some key with a child obj as its value and obj2 does not have the same key, then the method will be called with compare(obj1, undefined) which will throw an error at obj2[key]

– AZ_
12 hours ago





what if obj1 have some key with a child obj as its value and obj2 does not have the same key, then the method will be called with compare(obj1, undefined) which will throw an error at obj2[key]

– AZ_
12 hours ago













@loretoparisi updated

– adiga
12 hours ago





@loretoparisi updated

– adiga
12 hours ago













@adiga not sure, but the comments in the question do mention that, and the updated question as well, it cant be assumed that extra ket always have a string value.

– AZ_
12 hours ago






@adiga not sure, but the comments in the question do mention that, and the updated question as well, it cant be assumed that extra ket always have a string value.

– AZ_
12 hours ago














@AZ_ updated. Not sure if it will fail for any scenario

– adiga
12 hours ago





@AZ_ updated. Not sure if it will fail for any scenario

– adiga
12 hours ago











1














A recursive example,






var obj1 = 
prop1: 1,
prop2: "foo",
prop3:
prop4: 2,
prop5: "bar"
,
prop7: ,


var obj2 =
prop1: 3,
prop2: "foo",
prop3:
prop4: 2,
prop5: "foobar"
,
prop6: "new",
prop7: ,
prop8: ,


var result = ;

function compare(obj1, obj2, obj_)
for (let k in obj1)
var type = typeof obj1[k];
if (type === 'object')
obj_[k] = ;
if (!obj2[k])
obj_[k] = false;
else if ((Object.entries(obj1[k]).length === 0 && obj1[k].constructor === Object) && (Object.entries(obj2[k]).length === 0 && obj2[k].constructor === Object))
obj_[k] = true;
else
compare(obj1[k], obj2[k], obj_[k]);

else
obj_[k] = (obj1[k] === obj2[k]);





if (Object.keys(obj1).length < Object.keys(obj2).length) //check if both objects vary in length.
var tmp = obj1;
obj1 = obj2;
obj2 = tmp;


compare(obj1, obj2, result);

console.log(result);








share|improve this answer

























  • Thank you. If you try this case - jsfiddle.net/47fcqtom you will get a in the results for properties of type object that belong to both objects and that are .

    – loretoparisi
    10 hours ago







  • 1





    @loretoparisi I made a change, try now.

    – Shoyeb Sheikh
    10 hours ago















1














A recursive example,






var obj1 = 
prop1: 1,
prop2: "foo",
prop3:
prop4: 2,
prop5: "bar"
,
prop7: ,


var obj2 =
prop1: 3,
prop2: "foo",
prop3:
prop4: 2,
prop5: "foobar"
,
prop6: "new",
prop7: ,
prop8: ,


var result = ;

function compare(obj1, obj2, obj_)
for (let k in obj1)
var type = typeof obj1[k];
if (type === 'object')
obj_[k] = ;
if (!obj2[k])
obj_[k] = false;
else if ((Object.entries(obj1[k]).length === 0 && obj1[k].constructor === Object) && (Object.entries(obj2[k]).length === 0 && obj2[k].constructor === Object))
obj_[k] = true;
else
compare(obj1[k], obj2[k], obj_[k]);

else
obj_[k] = (obj1[k] === obj2[k]);





if (Object.keys(obj1).length < Object.keys(obj2).length) //check if both objects vary in length.
var tmp = obj1;
obj1 = obj2;
obj2 = tmp;


compare(obj1, obj2, result);

console.log(result);








share|improve this answer

























  • Thank you. If you try this case - jsfiddle.net/47fcqtom you will get a in the results for properties of type object that belong to both objects and that are .

    – loretoparisi
    10 hours ago







  • 1





    @loretoparisi I made a change, try now.

    – Shoyeb Sheikh
    10 hours ago













1












1








1







A recursive example,






var obj1 = 
prop1: 1,
prop2: "foo",
prop3:
prop4: 2,
prop5: "bar"
,
prop7: ,


var obj2 =
prop1: 3,
prop2: "foo",
prop3:
prop4: 2,
prop5: "foobar"
,
prop6: "new",
prop7: ,
prop8: ,


var result = ;

function compare(obj1, obj2, obj_)
for (let k in obj1)
var type = typeof obj1[k];
if (type === 'object')
obj_[k] = ;
if (!obj2[k])
obj_[k] = false;
else if ((Object.entries(obj1[k]).length === 0 && obj1[k].constructor === Object) && (Object.entries(obj2[k]).length === 0 && obj2[k].constructor === Object))
obj_[k] = true;
else
compare(obj1[k], obj2[k], obj_[k]);

else
obj_[k] = (obj1[k] === obj2[k]);





if (Object.keys(obj1).length < Object.keys(obj2).length) //check if both objects vary in length.
var tmp = obj1;
obj1 = obj2;
obj2 = tmp;


compare(obj1, obj2, result);

console.log(result);








share|improve this answer















A recursive example,






var obj1 = 
prop1: 1,
prop2: "foo",
prop3:
prop4: 2,
prop5: "bar"
,
prop7: ,


var obj2 =
prop1: 3,
prop2: "foo",
prop3:
prop4: 2,
prop5: "foobar"
,
prop6: "new",
prop7: ,
prop8: ,


var result = ;

function compare(obj1, obj2, obj_)
for (let k in obj1)
var type = typeof obj1[k];
if (type === 'object')
obj_[k] = ;
if (!obj2[k])
obj_[k] = false;
else if ((Object.entries(obj1[k]).length === 0 && obj1[k].constructor === Object) && (Object.entries(obj2[k]).length === 0 && obj2[k].constructor === Object))
obj_[k] = true;
else
compare(obj1[k], obj2[k], obj_[k]);

else
obj_[k] = (obj1[k] === obj2[k]);





if (Object.keys(obj1).length < Object.keys(obj2).length) //check if both objects vary in length.
var tmp = obj1;
obj1 = obj2;
obj2 = tmp;


compare(obj1, obj2, result);

console.log(result);








var obj1 = 
prop1: 1,
prop2: "foo",
prop3:
prop4: 2,
prop5: "bar"
,
prop7: ,


var obj2 =
prop1: 3,
prop2: "foo",
prop3:
prop4: 2,
prop5: "foobar"
,
prop6: "new",
prop7: ,
prop8: ,


var result = ;

function compare(obj1, obj2, obj_)
for (let k in obj1)
var type = typeof obj1[k];
if (type === 'object')
obj_[k] = ;
if (!obj2[k])
obj_[k] = false;
else if ((Object.entries(obj1[k]).length === 0 && obj1[k].constructor === Object) && (Object.entries(obj2[k]).length === 0 && obj2[k].constructor === Object))
obj_[k] = true;
else
compare(obj1[k], obj2[k], obj_[k]);

else
obj_[k] = (obj1[k] === obj2[k]);





if (Object.keys(obj1).length < Object.keys(obj2).length) //check if both objects vary in length.
var tmp = obj1;
obj1 = obj2;
obj2 = tmp;


compare(obj1, obj2, result);

console.log(result);





var obj1 = 
prop1: 1,
prop2: "foo",
prop3:
prop4: 2,
prop5: "bar"
,
prop7: ,


var obj2 =
prop1: 3,
prop2: "foo",
prop3:
prop4: 2,
prop5: "foobar"
,
prop6: "new",
prop7: ,
prop8: ,


var result = ;

function compare(obj1, obj2, obj_)
for (let k in obj1)
var type = typeof obj1[k];
if (type === 'object')
obj_[k] = ;
if (!obj2[k])
obj_[k] = false;
else if ((Object.entries(obj1[k]).length === 0 && obj1[k].constructor === Object) && (Object.entries(obj2[k]).length === 0 && obj2[k].constructor === Object))
obj_[k] = true;
else
compare(obj1[k], obj2[k], obj_[k]);

else
obj_[k] = (obj1[k] === obj2[k]);





if (Object.keys(obj1).length < Object.keys(obj2).length) //check if both objects vary in length.
var tmp = obj1;
obj1 = obj2;
obj2 = tmp;


compare(obj1, obj2, result);

console.log(result);






share|improve this answer














share|improve this answer



share|improve this answer








edited 10 hours ago

























answered 11 hours ago









Shoyeb SheikhShoyeb Sheikh

652211




652211












  • Thank you. If you try this case - jsfiddle.net/47fcqtom you will get a in the results for properties of type object that belong to both objects and that are .

    – loretoparisi
    10 hours ago







  • 1





    @loretoparisi I made a change, try now.

    – Shoyeb Sheikh
    10 hours ago

















  • Thank you. If you try this case - jsfiddle.net/47fcqtom you will get a in the results for properties of type object that belong to both objects and that are .

    – loretoparisi
    10 hours ago







  • 1





    @loretoparisi I made a change, try now.

    – Shoyeb Sheikh
    10 hours ago
















Thank you. If you try this case - jsfiddle.net/47fcqtom you will get a in the results for properties of type object that belong to both objects and that are .

– loretoparisi
10 hours ago






Thank you. If you try this case - jsfiddle.net/47fcqtom you will get a in the results for properties of type object that belong to both objects and that are .

– loretoparisi
10 hours ago





1




1





@loretoparisi I made a change, try now.

– Shoyeb Sheikh
10 hours ago





@loretoparisi I made a change, try now.

– Shoyeb Sheikh
10 hours ago

















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55591096%2fcompare-nested-objects-in-javascript-and-return-keys-equality%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Ружовы пелікан Змест Знешні выгляд | Пашырэнне | Асаблівасці біялогіі | Літаратура | НавігацыяДагледжаная версіяправерана1 зменаДагледжаная версіяправерана1 змена/ 22697590 Сістэматыкана ВіківідахВыявына Вікісховішчы174693363011049382

ValueError: Error when checking input: expected conv2d_13_input to have shape (3, 150, 150) but got array with shape (150, 150, 3)2019 Community Moderator ElectionError when checking : expected dense_1_input to have shape (None, 5) but got array with shape (200, 1)Error 'Expected 2D array, got 1D array instead:'ValueError: Error when checking input: expected lstm_41_input to have 3 dimensions, but got array with shape (40000,100)ValueError: Error when checking target: expected dense_1 to have shape (7,) but got array with shape (1,)ValueError: Error when checking target: expected dense_2 to have shape (1,) but got array with shape (0,)Keras exception: ValueError: Error when checking input: expected conv2d_1_input to have shape (150, 150, 3) but got array with shape (256, 256, 3)Steps taking too long to completewhen checking input: expected dense_1_input to have shape (13328,) but got array with shape (317,)ValueError: Error when checking target: expected dense_3 to have shape (None, 1) but got array with shape (7715, 40000)Keras exception: Error when checking input: expected dense_input to have shape (2,) but got array with shape (1,)

Illegal assignment from SObject to ContactFetching String, Id from Map - Illegal Assignment Id to Field / ObjectError: Compile Error: Illegal assignment from String to BooleanError: List has no rows for assignment to SObjectError on Test Class - System.QueryException: List has no rows for assignment to SObjectRemote action problemDML requires SObject or SObject list type error“Illegal assignment from List to List”Test Class Fail: Batch Class: System.QueryException: List has no rows for assignment to SObjectMapping to a user'List has no rows for assignment to SObject' Mystery