猿问

请教各位一个问题,求解答:js怎么遍历给对象赋值求老司机指点!

这看起来有点繁琐,可以简化也行,新手,谢谢大佬,感谢
getChartsData(this.rule).then(res=>{
letobj=res.data.data[0];
this.compRegion.lineCode=obj.lineCode;
this.compRegion.lineCodeName=obj.lineCodeName;
this.compRegion.timeRate=obj.timeRate;
this.compRegion.performanceRate=obj.performanceRate;
this.compRegion.percentRate=obj.percentRate;
this.compRegion.deviceRate=obj.deviceRate;
this.compRegion.deviceOee=obj.deviceOee;
this.compRegion.deviceTeep=obj.deviceTeep;
this.compRegion.startRate=obj.startRate;
this.compRegion.planRate=obj.planRate;
});
拉丁的传说
浏览 218回答 2
2回答

婷婷同学_

这样?getChartsData(this.rule).then(res=>{letobj=res.data.data[0];letneedKey=['lineCode','lineCodeName','timeRate','performanceRate','percentRate','deviceRate','deviceOee','deviceTeep','startRate','planRate']needKey.forEach(item=>{this.compRegion[item]=obj[item]})});

慕尼黑8549860

//解构this.compRegion={...res.data.data[0]}//如果还有一些多余的字段不想带进去可以使用only这库importonlyform'only'//前提this.compRegion有初始属性this.compRegion=only(res.data.data[0],Object.keys(this.compRegion))
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答