2017-01-02 2 views
-2
ない場合でも

例ツリーthis.props.rodexMatrixData、undefinedを返します

{ 
"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()は、データの配列を返すことになっています。Recursiv関数は常にそれが

何が間違っていますか?

+0

完全な例を挙げてください。 [mcve]を見てください。 –

答えて

1

getHierarchyDataは、ツリーのルートで検索対象の人物が見つからない場合は、関数の一番下にあり、undefinedを返します。それが木の根の人を見つけたらではない返すundefined。電話番号getHierarchyData(child,hierarchy.slice()...(理由は?)を呼び出すときにsliceを使用してコピーされ、コールの戻り値は破棄されます。ツリーの子ノードに人物が見つかったことを報告するコードはありません。

追加の再帰を中止し、でないツリーブランチを識別し、階層に含めるべきではないが見つかりました。発見されたフラグの実装はさまざまな方法で行うことができますが、以下のコードのhierarchy配列のプロパティにすぎません。

人を見つけた後返し、偽の枝のためのhierarchyオフのエントリをポップgetHierarchyDataための可能な構造、アプリケーションのオブジェクト構造に

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は歴史のようなものですが、私が探していたノードを見つけるまでノードを保存する必要があります。私がノードを見つけると、私は単純に履歴/パスをノードに返します。だから私は人ではない、私は彼への道が欲しい。 **これは関数の一番下にあり、未定義**を返しますが、ノードに達するとifに入ります。その後、それは単に再帰を返して終了します。 – Muco

+0

再帰関数からの戻りは、呼び出し直後の時点で実行を再開します。これが 'getHierarchyData'の前回の呼び出しの' if(tree.children) '分岐内にある場合、その(前の)呼び出しは' undefined'を返します。あなたはそれが何をしているかを見るために答えを試してみたいかもしれません。 – traktor53

+0

これでデータが返されましたが、必要な機能が失われました。スライスがなければ、私は正しいデータを得ることができません。 – Muco

0

クラス変数this.dataを作成しました。次に、ちょうど:

if (tree.staffNumber === this.props.selectedPerson.staffNumber) { 
    this.data= hierarchy; 
} 
関連する問題