2016-10-31 7 views
-2

私はツリー図を表すJSONを持っており、これをtreant.jsで視覚化できる新しい構造のJSONに変換したいと考えています。JSONを他の構造の新しいJSONに変換する

異なる数のノードで動作する必要があります。各ノード/子は0または2の子を持つことができます。これは実際に私の主な問題です。入力JSONを反復処理して、すべての子と子を得る方法は、各子のループを持たないでください。

また、親ノードのタイトルは、その子ノードでのみ表されますが、親ノードでも必要です。

入力:

treeModel = { 
"TreeModel": { 
    "AlgorithmName": "CART", 
    "Child": [{ 
     "RecordCount": 47, 
     "Score": "1", //should be the name 
     "ScoreDistribution": [ 
      [24, 23], //should be the description as string 
      [0.5106382978723404, 0.4893617021276596] 
     ], 
     "TreeNodePredicate": { 
      "FieldPredID": 0, 
      "FieldPredName": "Attribute1", //should be the title from parent 
      "FieldValue": 0, 
      "Operator": "EQUAL", 
      "PredicateType": "SimplePredicate" 
     } 
    }, { 
     "RecordCount": 214, 
     "Score": "0", //should be the name 
     "ScoreDistribution": [ 
      [2, 212], //should be the description as string 
      [0.009345794392523364, 0.9906542056074767] 
     ], 
     "TreeNodePredicate": { 
      "FieldPredID": 0, 
      "FieldPredName": "Attribute1", //should be the title from parent 
      "FieldValue": 1, 
      "Operator": "EQUAL", 
      "PredicateType": "SimplePredicate" 
     } 
    }], 
    "RecordCount": 261, 
    "Score": "0", //should be the name 
    "ScoreDistribution": [ 
     [26, 235], //should be the description as string 
     [0.09961685823754787, 0.9003831417624522] 
    ], 
    "TreeNodePredicate": { 
     "PredicateType": "TruePredicate" 
    } 
} 
} 

出力:

chart_config = { 
chart: { 
    container: "#tree-simple" 
}, 

nodeStructure: { 
    text: { 
     name: "0" , //Score 
     title: "Attribute1", //FieldPredName from Child 
     desc: "26, 235", //ScoreDistribution as String 
    }, 
    children: [ 
     { 
      text: { 
       name: "1" , //Score 
       title: "", //FieldPredName from Child(empty as no childs) 
       desc: "24, 23", //ScoreDistribution as String 
      } 
     }, 
     { 
      text: { 
       name: "0" , //Score 
       title: "", //FieldPredName from Child(empty as no childs 
       desc: "2, 212", //ScoreDistribution as String 
      } 
     } 
    ] 
} 
}; 
+0

これは、コード・書き込みサービスではありません、あなたが行き詰まったときに戻ってきて、あなたの問題を解決する自分を試してみてください。 – GingerPlusPlus

+0

値を再割り当てするコンストラクタに渡します。簡単です。 – Crowes

答えて

0

あなたが変換するための再帰的かつ反復的なアプローチを使用することができます。

function convert(source) { 
 
    var target = { 
 
      text: { 
 
       name: source.Score, 
 
       title: source.Child && source.Child[0] && source.Child[0].TreeNodePredicate && source.Child[0].TreeNodePredicate.FieldPredName || '', 
 
       desc: source.ScoreDistribution[0].join(', ') 
 
      } 
 
     }; 
 

 
    if (Array.isArray(source.Child)) { 
 
     target.children = source.Child.map(convert); 
 
    } 
 
    return target; 
 
} 
 

 
var treeModel = { TreeModel: { AlgorithmName: "CART", Child: [{ RecordCount: 47, Score: "1", ScoreDistribution: [[24, 23], [0.5106382978723404, 0.4893617021276596]], TreeNodePredicate: { FieldPredID: 0, FieldPredName: "Attribute1", FieldValue: 0, Operator: "EQUAL", PredicateType: "SimplePredicate" } }, { RecordCount: 214, Score: "0", ScoreDistribution: [[2, 212], [0.009345794392523364, 0.9906542056074767]], TreeNodePredicate: { FieldPredID: 0, FieldPredName: "Attribute1", FieldValue: 1, Operator: "EQUAL", PredicateType: "SimplePredicate" } }], RecordCount: 261, Score: "0", ScoreDistribution: [[26, 235], [0.09961685823754787, 0.9003831417624522]], TreeNodePredicate: { PredicateType: "TruePredicate" } } }, 
 
    chart_config = { chart: { container: "#tree-simple" }, nodeStructure: convert(treeModel.TreeModel) }; 
 

 
console.log(chart_config);
.as-console-wrapper { max-height: 100% !important; top: 0; }

関連する問題