语言 js
实现内容是把数据内name参数名替换成title参数名
- 数据结构
{
"id": "1",
"name": "1",
"children": [
{
"id": "2",
"name": "1.1",
"children": [
{
"id": "3",
"name": "1.1.1",
"children": []
},
{
"id": "4",
"name": "1.1.2",
"children": []
}
]
}
]
}
- 方法
function replaceParam(data) {
let dataList = [];
for (let item of data) {
if (item.children.length > 0) {
item.children = replaceParam(item.children);
}
let param = {
title: item.name,
id: item.id,
children: item.children
}
dataList.push(param)
}
return dataList;
}
- 效果
{
"id": "1",
"title": "1",
"children": [
{
"id": "2",
"title": "1.1",
"children": [
{
"id": "3",
"title": "1.1.1",
"children": []
},
{
"id": "4",
"title": "1.1.2",
"children": []
}
]
}
]
}