Compare nested objects in JavaScript and return keys equalityJavaScript: 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?

sed pattern replace " to " and to \ except json string

Is there a familial term for apples and pears?

What is the command to reset a Windows installation without deleting any files

Was there ever an axiom rendered a theorem?

What tool would a Roman-age civilisation use to reduce/breakup silver and other metals?

"To split hairs" vs "To be pedantic"

In What Way Would Cryomancy Affect the Food Eaten by Medieval People?

Why is the design of haulage companies so “special”?

Shall I use personal or official e-mail account when registering to external websites for work purpose?

Is there a name of the flying bionic bird?

Title of papers that have not been submitted to a venue?

Dual Citizen. Exited the US on Italian passport recently

If you're not a professional, what motivates you to keep writing?

Inflated grade on resume at previous job, might former employer tell new employer?

What is GPS' 19 year rollover and does it present a cybersecurity issue?

Is it possible for the two major parties in the UK to form a coalition with each other instead of a much smaller party?

How would photo IDs work for shapeshifters?

What is the purpose of trimming the bottom corners of TLC plates?

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

Extreme, but not acceptable situation and I can't start the work tomorrow morning

Pristine Bit Checking

Does it makes sense to buy a new cycle to learn riding?

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

What is the steepest angle that a canal can be traversable without locks?



Compare nested objects in JavaScript and return keys equality


JavaScript: 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;








6















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
    2 hours ago











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

    – loretoparisi
    2 hours ago


















6















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
    2 hours ago











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

    – loretoparisi
    2 hours ago














6












6








6


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 2 hours ago







loretoparisi

















asked 3 hours ago









loretoparisiloretoparisi

8,06654973




8,06654973







  • 1





    Will both objects always have exact match properties?

    – holydragon
    2 hours ago











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

    – loretoparisi
    2 hours ago













  • 1





    Will both objects always have exact match properties?

    – holydragon
    2 hours ago











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

    – loretoparisi
    2 hours ago








1




1





Will both objects always have exact match properties?

– holydragon
2 hours ago





Will both objects always have exact match properties?

– holydragon
2 hours ago













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

– loretoparisi
2 hours ago






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

– loretoparisi
2 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) => , 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) => 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)








share|improve this answer




















  • 1





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

    – loretoparisi
    1 hour ago











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

    – loretoparisi
    1 hour ago






  • 1





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

    – Nenad Vracar
    1 hour 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
    1 hour ago







  • 1





    @loretoparisi Try this jsfiddle.net/r0y8nd3q/1

    – Nenad Vracar
    39 mins ago


















4














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
    1 hour ago







  • 1





    without spread not Set.

    – Nina Scholz
    1 hour ago











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

    – loretoparisi
    1 hour ago











  • w h a t e v e r ...

    – Nina Scholz
    1 hour 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)
let value1 = obj1[key], value2 = obj2[key];

if (isObject(value1)

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
    2 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_
    2 hours ago











  • @loretoparisi updated

    – adiga
    2 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_
    2 hours ago












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

    – adiga
    2 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
    57 mins ago







  • 1





    @loretoparisi I made a change, try now.

    – Shoyeb Sheikh
    18 mins 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) => , 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) => 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)








share|improve this answer




















  • 1





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

    – loretoparisi
    1 hour ago











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

    – loretoparisi
    1 hour ago






  • 1





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

    – Nenad Vracar
    1 hour 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
    1 hour ago







  • 1





    @loretoparisi Try this jsfiddle.net/r0y8nd3q/1

    – Nenad Vracar
    39 mins 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) => , 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) => 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)








share|improve this answer




















  • 1





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

    – loretoparisi
    1 hour ago











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

    – loretoparisi
    1 hour ago






  • 1





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

    – Nenad Vracar
    1 hour 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
    1 hour ago







  • 1





    @loretoparisi Try this jsfiddle.net/r0y8nd3q/1

    – Nenad Vracar
    39 mins 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) => , 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) => 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)








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








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





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) => 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)






share|improve this answer














share|improve this answer



share|improve this answer








edited 25 mins ago

























answered 2 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
    1 hour ago











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

    – loretoparisi
    1 hour ago






  • 1





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

    – Nenad Vracar
    1 hour 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
    1 hour ago







  • 1





    @loretoparisi Try this jsfiddle.net/r0y8nd3q/1

    – Nenad Vracar
    39 mins ago












  • 1





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

    – loretoparisi
    1 hour ago











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

    – loretoparisi
    1 hour ago






  • 1





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

    – Nenad Vracar
    1 hour 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
    1 hour ago







  • 1





    @loretoparisi Try this jsfiddle.net/r0y8nd3q/1

    – Nenad Vracar
    39 mins ago







1




1





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

– loretoparisi
1 hour ago





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

– loretoparisi
1 hour ago













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

– loretoparisi
1 hour ago





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

– loretoparisi
1 hour ago




1




1





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

– Nenad Vracar
1 hour ago





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

– Nenad Vracar
1 hour 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
1 hour 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
1 hour ago





1




1





@loretoparisi Try this jsfiddle.net/r0y8nd3q/1

– Nenad Vracar
39 mins ago





@loretoparisi Try this jsfiddle.net/r0y8nd3q/1

– Nenad Vracar
39 mins ago













4














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
    1 hour ago







  • 1





    without spread not Set.

    – Nina Scholz
    1 hour ago











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

    – loretoparisi
    1 hour ago











  • w h a t e v e r ...

    – Nina Scholz
    1 hour ago















4














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
    1 hour ago







  • 1





    without spread not Set.

    – Nina Scholz
    1 hour ago











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

    – loretoparisi
    1 hour ago











  • w h a t e v e r ...

    – Nina Scholz
    1 hour ago













4












4








4







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 2 hours ago

























answered 2 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
    1 hour ago







  • 1





    without spread not Set.

    – Nina Scholz
    1 hour ago











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

    – loretoparisi
    1 hour ago











  • w h a t e v e r ...

    – Nina Scholz
    1 hour 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
    1 hour ago







  • 1





    without spread not Set.

    – Nina Scholz
    1 hour ago











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

    – loretoparisi
    1 hour ago











  • w h a t e v e r ...

    – Nina Scholz
    1 hour 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
1 hour 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
1 hour ago





1




1





without spread not Set.

– Nina Scholz
1 hour ago





without spread not Set.

– Nina Scholz
1 hour ago













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

– loretoparisi
1 hour ago





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

– loretoparisi
1 hour ago













w h a t e v e r ...

– Nina Scholz
1 hour ago





w h a t e v e r ...

– Nina Scholz
1 hour 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)
let value1 = obj1[key], value2 = obj2[key];

if (isObject(value1)

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
    2 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_
    2 hours ago











  • @loretoparisi updated

    – adiga
    2 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_
    2 hours ago












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

    – adiga
    2 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)
let value1 = obj1[key], value2 = obj2[key];

if (isObject(value1)

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
    2 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_
    2 hours ago











  • @loretoparisi updated

    – adiga
    2 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_
    2 hours ago












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

    – adiga
    2 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)
let value1 = obj1[key], value2 = obj2[key];

if (isObject(value1)

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)
let value1 = obj1[key], value2 = obj2[key];

if (isObject(value1)

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)
let value1 = obj1[key], value2 = obj2[key];

if (isObject(value1)

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)
let value1 = obj1[key], value2 = obj2[key];

if (isObject(value1)

return equality;


console.log(compare(obj1, obj2))






share|improve this answer














share|improve this answer



share|improve this answer








edited 1 hour ago

























answered 2 hours ago









adigaadiga

12.3k62645




12.3k62645












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

    – loretoparisi
    2 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_
    2 hours ago











  • @loretoparisi updated

    – adiga
    2 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_
    2 hours ago












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

    – adiga
    2 hours ago

















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

    – loretoparisi
    2 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_
    2 hours ago











  • @loretoparisi updated

    – adiga
    2 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_
    2 hours ago












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

    – adiga
    2 hours ago
















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

– loretoparisi
2 hours ago





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

– loretoparisi
2 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_
2 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_
2 hours ago













@loretoparisi updated

– adiga
2 hours ago





@loretoparisi updated

– adiga
2 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_
2 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_
2 hours ago














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

– adiga
2 hours ago





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

– adiga
2 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
    57 mins ago







  • 1





    @loretoparisi I made a change, try now.

    – Shoyeb Sheikh
    18 mins 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
    57 mins ago







  • 1





    @loretoparisi I made a change, try now.

    – Shoyeb Sheikh
    18 mins 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 19 mins ago

























answered 1 hour 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
    57 mins ago







  • 1





    @loretoparisi I made a change, try now.

    – Shoyeb Sheikh
    18 mins 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
    57 mins ago







  • 1





    @loretoparisi I made a change, try now.

    – Shoyeb Sheikh
    18 mins 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
57 mins 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
57 mins ago





1




1





@loretoparisi I made a change, try now.

– Shoyeb Sheikh
18 mins ago





@loretoparisi I made a change, try now.

– Shoyeb Sheikh
18 mins 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

How should I use the fbox command correctly to avoid producing a Bad Box message?How to put a long piece of text in a box?How to specify height and width of fboxIs there an arrayrulecolor-like command to change the rule color of fbox?What is the command to highlight bad boxes in pdf?Why does fbox sometimes place the box *over* the graphic image?how to put the text in the boxHow to create command for a box where text inside the box can automatically adjust?how can I make an fbox like command with certain color, shape and width of border?how to use fbox in align modeFbox increase the spacing between the box and it content (inner margin)how to change the box height of an equationWhat is the use of the hbox in a newcommand command?

Doxepinum Nexus interni Notae | Tabula navigationis3158DB01142WHOa682390"Structural Analysis of the Histamine H1 Receptor""Transdermal and Topical Drug Administration in the Treatment of Pain""Antidepressants as antipruritic agents: A review"

inputenc: Unicode character … not set up for use with LaTeX The Next CEO of Stack OverflowEntering Unicode characters in LaTeXHow to solve the `Package inputenc Error: Unicode char not set up for use with LaTeX` problem?solve “Unicode char is not set up for use with LaTeX” without special handling of every new interesting UTF-8 characterPackage inputenc Error: Unicode character ² (U+B2)(inputenc) not set up for use with LaTeX. acroI2C[I²C]package inputenc error unicode char (u + 190) not set up for use with latexPackage inputenc Error: Unicode char u8:′ not set up for use with LaTeX. 3′inputenc Error: Unicode char u8: not set up for use with LaTeX with G-BriefPackage Inputenc Error: Unicode char u8: not set up for use with LaTeXPackage inputenc Error: Unicode char ́ (U+301)(inputenc) not set up for use with LaTeX. includePackage inputenc Error: Unicode char ̂ (U+302)(inputenc) not set up for use with LaTeX. … $widehatleft (OA,AA' right )$Package inputenc Error: Unicode char â„¡ (U+2121)(inputenc) not set up for use with LaTeX. printbibliography[heading=bibintoc]Package inputenc Error: Unicode char − (U+2212)(inputenc) not set up for use with LaTeXPackage inputenc Error: Unicode character α (U+3B1) not set up for use with LaTeXPackage inputenc Error: Unicode characterError: ! Package inputenc Error: Unicode char ⊘ (U+2298)(inputenc) not set up for use with LaTeX