zoukankan      html  css  js  c++  java
  • 将后台数据数组对象(对象里边包含数组对象)---改为前端想要的数组对象--改变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 })

  • 相关阅读:
    【leetcode】416. Partition Equal Subset Sum
    【leetcode】893. Groups of Special-Equivalent Strings
    【leetcode】892. Surface Area of 3D Shapes
    【leetcode】883. Projection Area of 3D Shapes
    【leetcode】140. Word Break II
    【leetcode】126. Word Ladder II
    【leetcode】44. Wildcard Matching
    【leetcode】336. Palindrome Pairs
    【leetcode】354. Russian Doll Envelopes
    2017.12.22 英语面试手记
  • 原文地址:https://www.cnblogs.com/fdxjava/p/12174481.html
Copyright © 2011-2022 走看看