- 声明数组
newArr:[], oldArr:[ {mapId:'b1',imgPathLayer:'aaa'}, {mapId:'b2',imgPathLayer:'bbb'}, {mapId:'b3',imgPathLayer:'ccc'}, {mapId:'b4',imgPathLayer:'ddd'}, {mapId:'b1',imgPathLayer:'aaa'}, {mapId:'b4',imgPathLayer:'ddd'}, ],
- 双重for循环实现去重
async function initData() {
let oldArr = this.oldArr;
let newArr = this.newArr;
for(let i=0;i<oldArr.length;i++){
let flag = true;
// newArr 数组长度为零,跳过内循环执行下一步 if()函数
for(let j=0;j<newArr.length;j++){
console.log("- - - - - - - - - " + newArr.length);
if(oldArr[i].mapId === newArr[j].mapId){
// 如果newArr 数组中已存在相同的mapId 则不执行if() 函数
flag = false;
}
}
if(flag){
newArr.push(oldArr[i]);
console.log("--------" + newArr[i].mapId +" "+ newArr[i].imgPathLayer);
}
}
}
3.输出结果