首页 > 其他 > 详细

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

时间:2018-01-05 11:52:23      阅读:185      评论:0      收藏:0      [点我收藏+]
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;
}

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

原文:https://www.cnblogs.com/fight5/p/8203261.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!