var array1 = [{ Num: "A" }, { Num: "B" }];
var array2 = [
{ Num: "A", Name: "t1" },
{ Num: "B", Name: "t2" },
{ Num: "C", Name: "t3", age: "222" },
{ Num: "D", Name: "t4 ", age: "3333" },
];
var result = [];
1.for (var i = 0; i < array2.length; i++) {
var obj = array2[i];
var num = obj.Num;
var isExist = false;
for (var j = 0; j < array1.length; j++) {
var aj = array1[j];
var n = aj.Num;
if (n == num) {
isExist = true;
break;
}
}
if (!isExist) {
result.push(obj);
}
}
2.diff(arr1, arr2) {
let arr3 = [];
for (let key in arr1) {
let stra = arr1[key].id;
let count = 0;
for (let j = 0; j < arr2.length; j++) {
let strb = arr2[j].id;
if (stra == strb) {
count++;
}
}
if (count == 0) {
arr3.push(stra);
}
}
for (let key in arr2) {
let stra = arr2[key].id;
let count = 0;
for (let j = 0; j < arr1.length; j++) {
let strb = arr1[j].id;
if (stra == strb) {
count++;
}
}
if (count == 0) {
arr3.push(stra);
}
}
return arr3;
},
比较两个数组中不相等的对象
于 2023-05-29 17:07:27 首次发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)