利用解构赋值获取后端特定字段数据

很多时候,后端接口传过来的数据并不正好是我们需要的。有些场景下会有很多不需要的字段。

这时如果采用单个赋值的方法赋值数据无疑会比较麻烦。解决的办法就是利用解构赋值。

 mounted(){
      let objs ={
        name:'test',
        sex:'nan',
        caree:'kaifa',
        height:180,
        country:'country'
      };
      ({name:this.obj.name,caree:this.obj.caree}=objs);
      ({height:this.obj.height,country:this.obj.country}=objs);
  }

  假设上面objs是后端返回的数据

data () {
    return {
      obj:{}
    }
  }

  obj是定义好的data中的数据。那么就可以采用如下的方法进行赋值。

({name:this.obj.name,caree:this.obj.caree}=objs);
({height:this.obj.height,country:this.obj.country}=objs);

  

原文地址:https://www.cnblogs.com/zhensg123/p/11217327.html