2017-01-02 66 views
-2

例樹this.props.rodexMatrixData

{ 
"id": "41_0", 
"staffNumber": "800001", 
"firstName": "adsadsad", 
"lastName": "adssadsad", 
"role": "CEO", 
"phoneNumber": "+4453544534", 
"salary": 25000, 
"children": [ 
    { 
     "id": "42_0", 
     "staffNumber": "800010", 
     "firstName": "sda", 
     "lastName": "asaas", 
     "role": "CTO", 
     "phoneNumber": "+44874331234", 
     "salary": 20000, 
     "email": "[email protected]" 
    } 
    ] 
} 

代碼

incForFakeLevel(fakeLevel) { 
    return ++fakeLevel; 
} 

mapHierarchy(){ 
    let mappedHierarchy = []; 
    let hierarchyData = this.getHierarchyData(this.props.rodexMatrixData) //always undefined 

    if (hierarchyData){ 
     for (let person of hierarchyData){ 
      let margin = 10 * person.level; 
      mappedHierarchy.push(<div style={{marginLeft: `${margin}px`}}>{person.name}</div>) 
     } 
    } 

    return mappedHierarchy 
} 

getHierarchyData(tree, hierarchy = [], fakeLevel = 1){ 
    if (!this.props.selectedPerson) 
     return []; 

    hierarchy.push({ 
     name: `${tree.firstName} ${tree.lastName}`, 
     level: fakeLevel 
    }); 

    if (tree.staffNumber === this.props.selectedPerson.staffNumber){ 
     return hierarchy; // never undefined 
    } 


    if (tree.children){ 
     tree.children.forEach(child => 
      this.getHierarchyData(child,hierarchy.slice(), this.incForFakeLevel(fakeLevel)) 
     ); 
    } 
} 

getHierarchyData()總是返回undefined。但如果你的console.log 層次結構 getHierarchyData(),它從來沒有定義。 getHierarchyData() ist應該返回一個數組數組。Recursiv函數總是返回undefined,即使它不是

怎麼了?

+0

請提供一個完整的例子。看看[mcve]。 –

回答

1

如果getHierarchyData未在樹的根中找到正在搜索的人,則它將落在該函數的底部並返回undefined。如果它發現樹中根的人不是返回undefined。當呼叫getHierarchyData(child,hierarchy.slice()...(爲什麼?)時,通知hierarchy被複制使用slice,並且呼叫的返回值被丟棄:沒有代碼報告在樹的子節點中找到一個人。

似乎缺少的是一個「找到」標誌,以停止進一步的遞歸,並確定樹分支的人是而不是發現哪些不應該包括在層次結構中。實現找到的標誌可以通過不同的方式完成,但僅僅是下面代碼中hierarchy數組的屬性。

一個可能的結構getHierarchyData,找到後,一個人返回並彈出項關閉hierarchy假樹枝,

function getHierarchyData(tree, hierarchy = [], level = 1){ 
    if (!selectedPerson) 
     return []; 

    hierarchy.push({ 
     name: `${tree.firstName} ${tree.lastName}`, 
     level: level 
    }); 

    if (tree.staffNumber === selectedPerson.staffNumber){ 
     hierarchy.personFound = true; 
     return hierarchy; // never undefined 
    } 

    if (tree.children){ 
     for(let i = 0; i < tree.children.length; ++i) { 
      getHierarchyData(tree.children[i], hierarchy, level+1); 
      if(hierarchy.personFound) { 
       return hierarchy; 
      } 
     } 
    } 
    hierarchy.pop(); 
    return hierarchy; 
} 

引用到應用程序的對象結構沒有被包括在內,檢查人是否有重複樹中的記錄需要單獨驗證。

+0

我需要'hierarchy.slice()'來保存數據,但同時銷燬引用。 hirarchy就像一個歷史,我需要存儲的節點,直到我找到我的節點,我正在尋找。當我找到節點時,我只需將歷史/路徑返回給節點。所以我不是那個人,我想要走向他的道路。 **它落在函數的底部並返回undefined **,但當達到節點時,它進入if。之後它只是返回並結束遞歸。 – Muco

+0

從遞歸函數返回時,在緊接其調用後的位置恢復執行。如果這在以前調用'getHierarchyData'的'if(tree.children)'分支中,則該(之前)調用返回'undefined'。你不妨嘗試一下答案,看看它有什麼作用。 – traktor53

+0

現在它返回數據,但它失去了我想要的功能。沒有切片,我沒有得到正確的數據。 – Muco

0

解決bei創建類變量this.data。然後只是:

if (tree.staffNumber === this.props.selectedPerson.staffNumber) { 
    this.data= hierarchy; 
}