2

I have two objects, suppose

A = {
    name:'abc',
    age: 20,
    areaOfInterest:[
       { inSports:'badminton', outSports:'football' },
       { inSports:'chess', outSports:'tennis' }]
    }

B = {
    age: 20,
    name: 'abc',
    areaOfInterest:[
       { inSports:'chess', outSports:'tennis' },
       { inSports:'badminton', outSports:'football' }]
    }

As in given example above, the sequence of keys is different in both objects. Also, while comparing I dont want to go with

if(A.name == B.name)
if(A.areOfInterest.inSports == B.areOfInterest.inSports)

I want to compare them using loop like for...In or for...Of

Here is what I tried,

A = {
  name:'abc',
  age: 20,
  areaOfInterest:[
   { inSports:'badminton', outSports:'football' },
   { inSports:'chess', outSports:'tennis' }
  ]
}
    
B = {
  age:20,
  name: 'abc',
  areaOfInterest:[
   { inSports:'chess', outSports:'tennis' },
   { inSports:'badminton', outSports:'football' }
  ]
}
        
function objCompare(obj1, obj2){
  for (var [key, value] of Object.entries(obj1)) {
    for (var [k, v] of Object.entries(obj2)){
      if(k == key && v == value)
       console.log(true)
    }
  }
}

console.log(objCompare(A,B));

I am not getting true result. It gives undefined when it compares A.areOfInterest with B.areOfInterest

shaochuancs
  • 15,342
  • 3
  • 54
  • 62
Sameer
  • 103
  • 3
  • 12
  • 1
    "*the sequence of keys is different in both objects*" only in the object literal (i.e. the code). You should not expect object keys to be in any particular order, they are unordered. – RobG Jun 28 '17 at 02:22
  • Ok, how can I compare those objects anyhow? I am trying to iterate through both objects, not getting correct result – Sameer Jun 28 '17 at 02:23
  • 1
    Objects are only ever equal to themselves (using `==` or `===`). You can only directly compare primitives. – RobG Jun 28 '17 at 02:25
  • The problem I am getting is with comparing nested array inside objects A,B. – Sameer Jun 28 '17 at 02:26

2 Answers2

2

I'd do something like this:

A = {
  name:'abc',
  age: 20,
  areaOfInterest:[
   { inSports:'badminton', outSports:'football' },
   { inSports:'chess', outSports:'tennis' }
  ]
}
    
B = {
  age:'abc',
  name: 20,
  areaOfInterest:[
   { inSports:'chess', outSports:'tennis' },
   { inSports:'badminton', outSports:'football' }
  ]
}

C = {
  age:'abc',
  name: 20,
  areaOfInterest:[
   { inSports:'chess', outSports:'tennis' },
   { inSports:'badminton', outSports:'football' }
  ]
}
        
function objCompare(obj1, obj2){
  var same = true;
  for (var [key, value] of Object.entries(obj1)) {
    if(typeof value === 'object') {
      same = objCompare(obj1[key], obj2[key]);
    } else {
      if(obj1[key] != obj2[key]) same = false;
    }
  }
  
  return same;
}

console.log(objCompare(A,B));
console.log(objCompare(B,C));

So using the function recursively you can iterate over other objects inside the main objects.

Hope that helps you :)

JV Lobo
  • 5,526
  • 8
  • 34
  • 55
0

Another way to solve this problem is using lodash for object/array operation. The code is not as concise as recursion-solution, but it's more descriptive.

var A = {
  name:'abc',
  age: 20,
  areaOfInterest:[
   { inSports:'badminton', outSports:'tennis' },
   { inSports:'chess', outSports:'football' }
  ]
}
    
var B = {
  age:20,
  name: 'abc',
  areaOfInterest:[
   { inSports:'chess', outSports:'tennis' },
   { inSports:'badminton', outSports:'football' }
  ]
}
        
function objCompare(obj1, obj2){
  return _.isEqualWith(obj1, obj2, function(v1, v2, key) {
    if (key === undefined) {
      return undefined;
    }
    if (v1 === v2) {
      return true;
    }
    if (_.isArray(v1) && _.isArray(v2)) {
      if (_.isEmpty(_.differenceWith(v1, v2, _.isEqual))) {
        return true;
      }
    }
    
    return false;
  });
}

console.log(objCompare(A,B));
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.11.2/lodash.js"></script>
shaochuancs
  • 15,342
  • 3
  • 54
  • 62