- 对每一个children添加一个level,来表示它的层级
function deepGetValueItem(item, index) {
let deepIndex = index || 0
deepIndex += 1
for (let i = 0; i < item.length; i++) {
item[i]['level'] = deepIndex
if (item[i].children) deepGetValueItem(item[i].children, deepIndex)
}
}
- 每一层级下都与最外层的索引一致
function treeData(arr, sIndex = undefined) {
for (let i = 0; i < arr.length; i++) {
arr[i]._index = sIndex || String(i);
if (arr[i].children && arr[i].children.length > 0) {
treeData(arr[i].children, arr[i]._index);
}
}
}
treeData(data);
网友评论