循环中push覆盖数据问题记录

var showData=[];
let show1={Id:'',SeriesName:'',ProductCategory:[]};
let show2={Id:'',SeriesName:''};
if(res.data.Code==200){
for(let i=0;i<result.length;i++){

show1.Id=result[i].Id;
show1.SeriesName=result[i].SeriesName;
for(let j=0;j<result[i].ProductCategory.length;j++){

show2.Id=result[i].ProductCategory[j].Id;
show2.SeriesName=result[i].ProductCategory[j].CategoryName;
show1.ProductCategory.push(show2);
}
showData.push(show1);
}

像这样写是会覆盖的,因为地址没变。所以为了每次循环都有新的地址要这样写:

if(res.data.Code==200){
for(let i=0;i<result.length;i++){
let show1={Id:'',SeriesName:'',ProductCategory:[]};
show1.Id=result[i].Id;
show1.SeriesName=result[i].SeriesName;
for(let j=0;j<result[i].ProductCategory.length;j++){
let show2={Id:'',SeriesName:''};
show2.Id=result[i].ProductCategory[j].Id;
show2.SeriesName=result[i].ProductCategory[j].CategoryName;
show1.ProductCategory.push(show2);
}
showData.push(show1);
}
this.options2=showData;
}
posted @ 2018-01-05 11:14  三类鱼池猫  阅读(1065)  评论(0编辑  收藏  举报