将后台数据数组对象(对象里边包含数组对象)---改为前端想要的数组对象--改变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次" }] }]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | 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 })
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?