-1

I need to compare below two JSON objects.

const object1 = {
name: 'aman',
address: [{
houseNo: '123',
city: 'city1',
}],
interest: ['cooking', 'playing']
};

const object2 = {
name: 'aman',
address: [{
city: 'city1',
houseNo: '123',
}],
interest: ['cooking', 'playing']
};

I need to create a function to compare these objects.

Thanks in advance.

aman jain
  • 165
  • 1
  • 1
  • 7
  • 1
    Please learn [the difference between JSON and the Object Literal Notation](https://stackoverflow.com/questions/2904131/what-is-the-difference-between-json-and-object-literal-notation). – str Jun 28 '20 at 08:20
  • 1
    What should be the result? true or false if objects are identical or not? – wizardofOz Jun 28 '20 at 08:20
  • Does this answer your question? [Object comparison in JavaScript](https://stackoverflow.com/questions/1068834/object-comparison-in-javascript) – Sebastian Simon Jun 28 '20 at 08:21
  • Also please read [How do I ask a good question](https://stackoverflow.com/help/how-to-ask). Currently, your question does not show any research effort and is unclear. – str Jun 28 '20 at 08:22
  • The result could be boolean. – aman jain Jun 28 '20 at 08:23
  • Related: [Does ES6 introduce a well-defined order of enumeration for object properties?](https://stackoverflow.com/questions/30076219/does-es6-introduce-a-well-defined-order-of-enumeration-for-object-properties) Would you expect your example to return true or false? – str Jun 28 '20 at 08:23
  • Please create function in ES6 and compare nested objects. – aman jain Jun 28 '20 at 08:24

2 Answers2

1

As people suggest, I agree this is not a good way to ask this. But still, there are people who struggle with this

(I totally did when I had started years ago)

Short answer: Use some lib like lodash or other libs that give a simple way to do (suggested)

    var object1 = { ... };
    var object2 = { ... };
 
    _.isEqual(object1, object2);
    // => true
 
    object1 === object2;
    // => false

can use pc_coder's solution if want to avoid importing a lib

Reason:

Object in JS != JSON

You can't equate two objects straight away as it will not compare its value but the reference to it. (Hence, the above solution applies only if you intend it to use obj1 & obj2 as just JSON objects)

0

const object1 = {name: 'aman', address: [{houseNo:'123',city: 'city1'}],interest: ['cooking','playing']};
const object2 = {name: 'aman', address: [{city: 'city1',houseNo: '123'}],interest: ['cooking','playing']};
const object3 = {name: 'aman', address: [{city: 'city1',houseNo: '123'}],interest: ['playing','cooking']};
const object4 = {name: 'aman2',address: [{city: 'city1',houseNo: '123'}],interest: ['playing','cooking']};
const object5 = {name: 'aman2',address: [{city: 'city1',houseNo: '123',test:"deneme"}],interest: ['playing','cooking']};
const object6 = {name: 'aman2',address: [{city: 'city1',houseNo: '123'}],interest:['playing','cooking','test']};



console.log("expected:true, result:",  compare(object1,object2))
console.log("expected:true, result:",  compare(object1,object3))
console.log("expected:false, result:", compare(object1,object4))
console.log("expected:false, result:", compare(object1,object5))
console.log("expected:false, result:", compare(object1,object6))

function compare(obj1,obj2){
  if(Array.isArray(obj1)){obj1=obj1.sort();obj2=obj2.sort()}
  return typeof obj1 === 'object' && Object.keys(obj1).length > 0 
  ? Object.keys(obj1).length === Object.keys(obj2).length 
      && Object.keys(obj1).every(p => compare(obj1[p], obj2[p]))
  : obj1 === obj2
}
mr. pc_coder
  • 16,412
  • 3
  • 32
  • 54