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

How to compare two objects with nested array of object using loop

本问题已经有最佳答案,请猛点这里访问。

假设我有两个物体

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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' }]
    }

如上面的示例所示,两个对象中键的顺序是不同的。另外,在比较的时候,我不想和

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

我想用循环比较它们,比如

这是我试过的,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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时,它给出未定义的


我会这样做:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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));

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

希望能帮到你:)


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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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));
1
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.11.2/lodash.js">