如何使用循环将两个对象与嵌套的对象数组进行比较

时间:2017-06-28 02:18:46

标签: javascript arrays ecmascript-6 javascript-objects object-comparison

我有两个对象,假设

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' }]
    }

如上面给出的例子中,两个对象中的键序列是不同的。此外,虽然比较我不想与

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

我想使用循环来比较它们......用于...或用于...

这是我尝试过的,

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

我没有得到真实的结果。当它将A.areOfInterest与B.areOfInterest

进行比较时,它给出了undefined

2 个答案:

答案 0 :(得分:2)

我做这样的事情:



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




因此,递归使用函数可以迭代主对象内的其他对象。

希望能帮助你:)

答案 1 :(得分:0)

解决此问题的另一种方法是使用lodash进行对象/数组操作。代码不像递归解决方案那样简洁,但它更具描述性。



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>
&#13;
&#13;
&#13;