源代码:
1 // 树状数组指定位置增加子节点(必须标识为key) 2 addChildren({ arr, value, item }) { 3 let newArr = [] 4 arr.forEach(ele => { 5 if (ele.key === value) { 6 ele.children.push(item) 7 newArr.push(ele) 8 } else { 9 if (ele.children.length > 0) { 10 ele.children = this.addChildren({ 11 arr: ele.children, 12 value, 13 item 14 }) 15 newArr.push(ele) 16 } else { 17 newArr.push(ele) 18 } 19 } 20 }) 21 return newArr 22 }
参数解析:{
arr: 待处理数组
value: 待添加子节点的key的值
item: 待添加的节点数据
}