最近碰到一个需求,新增了数据,重新调一下接口,产品要求列表定位在新增的那一条。可是后端往里面加数据的时候是随机加的,所以需要比较新增前后两个对象数组不同的索引,方便定位。
可以在watch需要监听的数组里这样写。
//这是用双层for循环
let array1 = [{
"USER_ID": "1111",
"USER_NAME": "88881",
"TF_REMARK": "备注备注备注"
}, {
"USER_ID": "2222",
"USER_NAME": "88882",
"TF_REMARK": "这是一个备注"
}];
let array2 = [
{
"USER_ID": "1111",
"USER_NAME": "88881",
"TF_REMARK": "备注备注备注"
},
{
"USER_ID": "2222",
"USER_NAME": "88881",
"TF_REMARK": "备注备注备注"
},
{
"USER_ID": "3333",
"USER_NAME": "88882",
"TF_REMARK": "这是一个备注"
},
{
"USER_ID": "4444",
"USER_NAME": "8888",
"TF_REMARK": null
}
];
let result = [];
for (let i = 0; i < array2.length; i++) {
let obj = array2[i];
let num = obj.USER_ID;
let flag = false;
for (let j = 0; j < array1.length; j++) {
let aj = array1[j];
let n = aj.USER_ID;
if (n == num) {
flag = true;
break;
}
}
if (!flag) {
result.push(item1);
}
}
console.log('result',result);
// 这里用forEatch循环
array2.forEach((item1, index1) => {
console.log(item1)
let checkBool = false
array1.forEach((item2, index2) => {
if (item1.USER_ID=== item2.USER_ID) {
checkBool = true
}
})
if (!checkBool) {
result.push(item1);
}
})
console.log('result',result);