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;
}