将后台数据数组对象(对象里边包含数组对象)---改为前端想要的数组对象--改变key值(替换)

左边获取的数据
[{
    "member": "811中队",
    "course_list": [{
        "course_id": 852,
        "course_name": "深蹲跳60次",
        "course_times": "1次"
    }]
}, {
    "member": "812中队",
    "course_list": [{
        "course_id": 853,
        "course_name": "高抬腿跑",
        "course_times": "3次"
    }]
}]
想要的格式
[{
    "name": "811中队",
       "key": 15,
    "children": [{
        "key": 852,
        "title": "深蹲跳60次",
        "course_times": "1次"
    }]
}, {
    "name": "812中队",
       "key": 16,
    "children": [{
        "key": 853,
        "title": "高抬腿跑",
        "course_times": "3次"
    }]
}]
           for (var j = 0; j < res.data.length; j++) {
              // 应该返回的还有一个部门id,拼接到children的上边835_部门id
              var dict =
                {
                  title: '',
                  key: 0,
                  children: [{
                    key: 0,
                    title: '',
                    course_times: '0次'
                  }]
                }
              console.log(res.data[j])
              dict.title = res.data[j].member
              dict.key = 2
              // var dict = []
              dict.children[0].key = res.data[j].course_list[0].course_id
              dict.children[0].title = res.data[j].course_list[0].course_name + '-' + res.data[j].course_list[0].course_times
              this.treeData.push(dict)
              console.log(dict)
            }
            console.log(this.treeData)
            console.log('循环结束的最后数据' + JSON.stringify(this.treeData))

  

js修改数组对象的属性(key)名

例如:把如下data1的key名称修改成data2中的key名称

data1: [ { appName: '应用1', capacity: 233456 }, { title: '应用2', key: 124535 }]

data2: [ { name: '应用1', value: 233456 }, { name: '应用2', value: 124535 } ]
方法:

var data2=[];
data1.map((value,index,arry)=>{
data2.push({ 'name': value.latitude, 'value': value.longitude })

posted @ 2020-01-10 09:03  小白咚  阅读(2830)  评论(0编辑  收藏  举报