// 获取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);
参考文章:https://blog.csdn.net/Lucky_Q...