事情是这样的:原一个购物车 合并本地数据和服务器 数据方法如下 ,正常测试没有问题,当每次重新登录,会调用到这个方法,就会莫名其妙的卡主,debug 发现 a1.length =800000 导致卡死
但是 console.log(a1) 长度是正常的。。。
async getMergeCart(){ let a1 = await this.getLocalCart();//本地数据 let a2 = await this.getServeCart();//服务器数据 let arr=a1;//先赋值给arr a1 console.log('debug',a1); console.log('debug',a2); if(a1.length === 0){ return a2;//如果本地没数据 返回服务器数据。。。。 } for(let i=0;i<a1.length;i++){ for(let j=0;j<a2.length;j++){ if(a1[i].id!=a2[j].id){ arr.push(a2[j]); } } } return arr; }
后来将let 改为了const 就没有出现这个 问题了,这是什么情况
async getMergeCart(){ const a1 = await this.getLocalCart();//本地数据 const a2 = await this.getServeCart();//服务器数据 let arr=a1;//先赋值给arr a1 console.log('debug',a1); console.log('debug',a2); if(a1.length === 0){ return a2;//如果本地没数据 返回服务器数据。。。。 } for(let i=0;i<a1.length;i++){ for(let j=0;j<a2.length;j++){ if(a1[i].id!=a2[j].id){ arr.push(a2[j]); } } } return arr; }