// 获取columns最大深度
function getArrMaxDeep(arr, childKey){
let deepArr = new Array(arr.length).fill(0);
function getDeep(data, i, deepArr){
//获取当前结点的子数组,并且打印当前结点的值
var treeRoot = data[childKey];
//如果当前结点没有子数组了(没有子结点)就跳出当前递归,并且使计数器+1,并把计数器i的值存入深度数组中
if(!treeRoot){
i++;
deepArr.push(i);
return;
}
//如果当前结点有子数组,就要使计数器+1
i++;
//通过for循环来找出每一条路径,对遍历到的结点使用递归
for(let j=0;j<treeRoot.length;j++){
getDeep(treeRoot[j], i, deepArr); //递归时传入的就是当前结点的第j个子结点,当这第j个子结点中的所有子孙结点全部遍历完成之后,再去遍历第j+1个结点的所有子孙结点
}
}
arr.forEach((e, endex) => {
let momArr = [];
getDeep(e, 0, momArr);
deepArr[endex] = Math.max(...momArr);
});
return Math.max(...deepArr);
}
var columns = [
{ title: "key1", dataIndex: "key1", width: 100 },
{ title: "key2", showKey: "key2", children: [
{ title: "key21" },
{ title: "key22" },
{ title: "key23" },
{ title: "key24" },
{ title: "key25" },
{ title: "key26" }
]}
];
var maxDeep = getArrMaxDeep(columns, "children");
console.log(maxDeep);
24 天前