左邊獲取的數據 [{ "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 })