Convert the data format of the array to the key as the key and the name as the value to the backend

Problem Description

Demand: sorting number when modifying, tone modify the interface, the data need to be changed idis a bond, sequencepassed to the backend data format values

const list =[{
    
    id:'t12365',sequence:1,code:'ggg'},{
    
    id:'t14532',sequence:3,code:'aaa'}]

需要的数据格式:{
    
    't12365':1,'t14532':3}
// ,如下,arrs 是从后台获取到的数据,且后续需将该数据转成key为键,name为值的数组数据格式传给后端。
 mounted() {
    
    
    let arrs = [
      {
    
     key: "A", name: "答案A" },
      {
    
     key: "B", name: "答案B" },
      {
    
     key: "C", name: "答案C" }
    ];
    console.log(this.convertData(this.arrs));// {A: "答案A", B: "答案B", C: "答案C"}
  },
  methods: {
    
    
    convertData(arrs) {
    
    
      let data = {
    
    };
      arrs.map(val => {
    
    
        this.$set(data, val.key, val.name); // 用的vue的set方法
      });  
      return data;
    }
 }      

Guess you like

Origin blog.csdn.net/ddx2019/article/details/108280845