在开发过程中,往往会遇到后端返回来的数组字段,和前端用的不一致的现象,如果组是这样的倒还好
arr:[{id:'1',name:'小明'},{id:'2',name:'小红'}
但如果是这样呢
arr:[{id:'0',name:'一级',sub:[{pid:'1',id:'2',name:'二级',sub:[{pid:'2',id:'3',name:'三级'}]}]}]
遇到这种情况如果是单纯的遍历、push就要麻烦一些,不妨用下下面的骚操作来处理!很暴力!
async getcate(){
const rs = await this.$request('myproduct', 'catesData');
this.catelist=rs
this.catelist=JSON.parse(JSON.stringify(this.catelist).replace(/list/g,"children"));
this.catelist=JSON.parse(JSON.stringify(this.catelist).replace(/id/g,"value"));
this.catelist=JSON.parse(JSON.stringify(this.catelist).replace(/name/g,"label"));
},
(注意:/list/g,里面的list是后端返回来的字段名,后面的’children'是自己需要的字段)