/**
?????*?該方法用于將有父子關系的數組轉換成樹形結構的數組
?????*?接收一個具有父子關系的數組作為參數
?????*?返回一個樹形結構的數組
????*/
????translateDataToTree(data){
??????//?沒有父節點的數據
??????let?parents?=?data.filter(value?=>?value.upperCompanyCode?==?0);
??????//?有父節點的數據
??????let?children?=?data.filter(value?=>?value.upperCompanyCode?!==?'undefined'?&&?value.upperCompanyCode?!=?null);
??????//?定義轉換方法的具體實現
??????let?translator?=?(parents,children)?=>?{
????????//?遍歷父節點數據
????????parents.forEach((parent)?=>?{
??????????//?遍歷子節點數據
??????????children.forEach((current,index)?=>?{
????????????//?此時找到父節點對應的一個子節點
????????????if(current.upperCompanyCode?===?parent.comCode){
??????????????//?對子節點數據進行深拷貝,這里只支持部分類型
??????????????let?temp?=?JSON.parse(JSON.stringify(children));
??????????????//?讓當前子節點從temp中移除,temp作為新的子節點數據,這里是為了遞歸時,子節點的遍歷次數更少,如果父子關系的層級越多,越有利
??????????????temp.splice(index,1);
??????????????//?讓當前子節點作為唯一的父節點,去遞歸查找其對應的子節點
??????????????translator([current],?temp);
??????????????typeof?parent.children?!==?'undefined'???parent.children.push(current)?:?parent.children?=?[current]
????????????}
??????????})
????????})
??????};
??????//?調用轉換方法
??????translator(parents,children);
??????//?返回結果
??????return?parents;
????},