js比较两个数组对象,取出不同的值
程序员文章站
2024-03-15 22:09:00
...
js比较两个数组对象,取出不同的值
先搬运
var array1 = [ {"Num": "A " },{"Num": "B" }];
var array2 = [ {"Num": "A ","Name": "t1 " }, {"Num": "B","Name": "t2"}, {"Num": "C " ,"Name": "t3 "}];
var result = [];
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);
}
}
console.log(result);
$scope.datacaseNodeArr = [];
for(var i = 0; i < datacaseRes.length; i++){
var isExist = false;
for(var j = 0; j < flowResData.length; j++){
if(flowResData[j].id == datacaseRes[i].id){
isExist = true;
break;
}
}
if(!isExist){
$scope.datacaseNodeArr.push(datacaseRes[i]);
}
}
```
然后用到的:
for循环中的值只能赋一个(重要)
for (var i in this_.proEcho) {
var isExist = false;
if (this_.proEcho[i].custno != "") {
for (var j in this_.tableData3) {
if (
this_.proEcho[i].custno == this_.tableData3[j].personCustno
) {
this_.requParams.proCustno.push(this_.proEcho[i].custno);
isExist = true;
break;
}
} //重点
if(!isExist){
this_.requParams.proCustno.push(this_.proEcho[i].name);
this_.editEcho.push({
custno: this_.proEcho[i].custno,
inviteCustno: this_.proEcho[i].inviteCustno,
metd: this_.proEcho[i].metd,
ctco: this_.proEcho[i].ctco,
proRole: "1",
name: this_.proEcho[i].name
});
break;
}
} else {
this_.requParams.proCustno.push(this_.proEcho[i].name);
this_.editEcho.push({
custno: this_.proEcho[i].custno,
inviteCustno: this_.proEcho[i].inviteCustno,
metd: this_.proEcho[i].metd,
ctco: this_.proEcho[i].ctco,
proRole: "1",
name: this_.proEcho[i].name
});
}
}
或者
for (var i in this_.proEcho) {
if (this_.proEcho[i].custno != "") {
var obj = this_.proEcho[i];
var num = obj.custno;
var isExist = false;
for (var j in this_.tableData3) {
var aj = this_.tableData3[j];
var n = aj.personCustno;
if (n == num) {
isExist = true;
this_.requParams.proCustno.push(this_.proEcho[i].custno);
break;
}
}
if (!isExist) {
this_.requParams.proCustno.push(this_.proEcho[i].name);
this_.editEcho.push({
custno: this_.proEcho[i].custno,
inviteCustno: this_.proEcho[i].inviteCustno,
metd: this_.proEcho[i].metd,
ctco: this_.proEcho[i].ctco,
proRole: "1",
name: this_.proEcho[i].name
});
}
} else {
this_.requParams.proCustno.push(this_.proEcho[i].name);
this_.editEcho.push({
custno: this_.proEcho[i].custno,
inviteCustno: this_.proEcho[i].inviteCustno,
metd: this_.proEcho[i].metd,
ctco: this_.proEcho[i].ctco,
proRole: "1",
name: this_.proEcho[i].name
});
}
}