如何使用循环将两个对象与嵌套的对象数组进行比较
How to compare two objects with nested array of object using loop
我有两个对象,假设
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)
我想使用 for...In 或 for...Of
之类的循环来比较它们
这是我试过的,
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
时给出未定义
我会这样做:
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 进行 object/array 操作。代码没有递归解法那么简洁,但是描述性更强。
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>
我有两个对象,假设
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)
我想使用 for...In 或 for...Of
之类的循环来比较它们这是我试过的,
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
时给出未定义我会这样做:
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 进行 object/array 操作。代码没有递归解法那么简洁,但是描述性更强。
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>