修改后台返回数据的字段

由于项目用的elementui使用的其中tree插件

后台返回的数据与插件要求的数据字段不符导致无法正常渲染    所以前端得修改返回数据的字段名

方法

   companylist().then((res) => {
      console.log(res)
      var newdata = JSON.parse(JSON.stringify(res.data).replace(/name/g, 'label').replace(/sub/g, 'children'))
      this.datas = newdata
    }).catch((err) => {});

 

解释:1)JSON.stringify()把json对象转成json字符串;

          2)使用正则的replace()方法替换属性名;

          3)JSON.parse()把json字符串又转成json对象。

原文地址:https://www.cnblogs.com/xiaohuohuai/p/13853046.html