大佬们遇到过这个问题吗?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;
});
慕姐8265434
浏览 325回答 2
2回答

慕勒3428872

这样?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]})});

收到一只叮咚

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

相关分类

JavaScript