2016-11-09 4 views
1

私は実験として現時点でD3を使用していますが、特定の形式でAPIからデータを受け取りますが、そのデータを次のD3で動作する形式に再構成する必要があります私が受け取っているデータのD3が期待されてstrucutreは以下の通りです新しい構造体にデータを書式設定する

{ 
    "user_id": 3, 
    "service_user": "Phillippa", 
    "staff_name": "Abbey", 
    "role": "A", 
    "staff_id": 2, 
    "status": "P", 
    "workbase": "Workbase 1" 
}, 
{ 
    "user_id": 60, 
    "service_user": "Raymond", 
    "staff_name": "Adam", 
    "role": "D", 
    "staff_id": 8, 
    "status": "C", 
    "workbase": "Workbase 2" 
}, 
{ 
    "user_id": 63, 
    "service_user": "Alison", 
    "staff_name": "Adam", 
    "role": "D", 
    "staff_id": 8, 
    "status": "C", 
    "workbase": "Workbase 2" 
}, 
{ 
    "user_id": 68, 
    "service_user": "Philippa", 
    "staff_name": "Adam", 
    "role": "C", 
    "staff_id": 9, 
    "status": "C", 
    "workbase": "Workbase 2" 
}, 
{ 
    "user_id": 57, 
    "service_user": "Philip", 
    "staff_name": "Adam", 
    "role": "W", 
    "staff_id": 9, 
    "status": "C", 
    "workbase": "Workbase 2" 
} 

{ 
    "name":"flare", 
    "children":[ 
     { 
      "name":"analytics", 
      "children":[ 
       { 
        "name":"cluster", 
        "children":[ 
         { 
          "name":"AgglomerativeCluster", 
          "size":3938 
         }, 
         { 
          "name":"CommunityStructure", 
          "size":3812 
         }, 
         { 
          "name":"HierarchicalCluster", 
          "size":6714 
         }, 
         { 
          "name":"MergeEdge", 
          "size":743 
         } 
        ] 
       }, 
       { 
        "name":"graph", 
        "children":[ 
         { 
          "name":"BetweennessCentrality", 
          "size":3534 
         }, 
         { 
          "name":"LinkDistance", 
          "size":5731 
         }, 
         { 
          "name":"MaxFlowMinCut", 
          "size":7840 
         }, 
         { 
          "name":"ShortestPaths", 
          "size":5914 
         }, 
         { 
          "name":"SpanningTree", 
          "size":3416 
         } 
        ] 
       }, 
       { 
        "name":"optimization", 
        "children":[ 
         { 
          "name":"AspectRatioBanker", 
          "size":7074 
         } 
        ] 
       } 
      ] 
     } 
    ] 
} 

だから私は基本的に子供アレイを持つオブジェクトされた新しい構造を生成するために、受信したデータを使用する必要があります。

最初の構造体の仕組みは、Workbase 1に1つの子「Abbey」があり、「Abbey」には1つの子「Phillipa」があります。今度は、Workbase 1が返されたデータに何度も現れることがあります。 Workbase 1の特定のオブジェクトにプッシュする必要があります。

Workbase 2は少し複雑です。Workbase 2には1人の子供 "Adam"があり、 "Adam"には4人の子供 "Raymond"、 "Allison"、 "Phillipa"、 "Phillip"があります。

理論的に

データは次のようになり、

{ 
    "name":"Workbase 1", 
    "children":[ 
     { 
      "name":"Abbey", 
      "children":[ 
       { 
        "name":"Phillipa" 
       } 
      ] 
     } 
    ] 
}, 
{ 
    "name":"Workbase 2", 
    "children":[ 
     { 
      "name":"Adam", 
      "children":[ 
       { 
        "name":"Raymond" 
       }, 
       { 
        "name":"Allison" 
       }, 
       { 
        "name":"Phillipa" 
       }, 
       { 
        "name":"Phillip" 
       } 
      ] 
     } 
    ] 
} 

これまでのところ私は、オブジェクトをループして、私は唯一のユニークなworkbasesを取得しています確認しています、

original_data.forEach(function(j){ 
    if(_.indexOf(workbases, j.workbase) < 0) { 
     workbases.push(j.workbase); 

     data.push({ 
      name : j.workbase, 
      children : [] 
     }); 
    } 
}); 

この時点から私は働くことができません正しい子供と子供の子供たちを正しい作業台に入れるために、どんなアイディアですか?

+0

データのどの部分が子供であると親が何であるかを決定? AbbeyとPhillipa、またはAdamとRaymond、Allison、Phillipa、Phillipとの関係 – mikkelrd

+0

最初と3番目のデータサンプルは有効な形式ではありません。私はそれぞれを配列にラップする必要があると仮定します(角括弧; ']') – JAAulde

答えて

0

ネストされたアプローチで名前のハッシュテーブルを使用できます。

var data = [{ "user_id": 3, "service_user": "Phillippa", "staff_name": "Abbey", "role": "A", "staff_id": 2, "status": "P", "workbase": "Workbase 1" }, { "user_id": 60, "service_user": "Raymond", "staff_name": "Adam", "role": "D", "staff_id": 8, "status": "C", "workbase": "Workbase 2" }, { "user_id": 63, "service_user": "Alison", "staff_name": "Adam", "role": "D", "staff_id": 8, "status": "C", "workbase": "Workbase 2" }, { "user_id": 68, "service_user": "Philippa", "staff_name": "Adam", "role": "C", "staff_id": 9, "status": "C", "workbase": "Workbase 2" }, { "user_id": 57, "service_user": "Philip", "staff_name": "Adam", "role": "W", "staff_id": 9, "status": "C", "workbase": "Workbase 2" }], 
 
    result = []; 
 

 
data.forEach(function (a) { 
 
    var check = function (key, target) { 
 
      if (!this[key]) { 
 
       this[key] = { name: key, children: [] }; 
 
       target.push(this[key]); 
 
      } 
 
     }.bind(this); 
 

 
    check(a.workbase, result); 
 
    check(a.staff_name, this[a.workbase].children); 
 
    this[a.staff_name].children.push({ name: a.service_user }); 
 
}, Object.create(null)); 
 

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

0
let sorted = original_data.reduce((acc, cur) => { 
    /* check if the workbase is already in the array */ 
    let wb = acc.find(wb => wb.name === cur.workbase); 
    if (wb) { 
    /* check if staff by that name is already in this workbase */ 
    let staff = wb.children.find(staff => staff.name === cur.staff_name); 
    if (staff) { 
     staff.children.push({name: cur.service_user}); 
    } else { 
     /* if not, instantiate this staff in the array */ 
     wb.push({ 
     name: cur.staff_name, 
     children: [{name: cur.service_user}] 
     }); 
    } 
    } else { 
    /* if not, instantiate this workbase in the array */ 
    acc.push({ 
     name: cur.workbase, 
     children: [ 
     { 
      name: cur.staff_name, 
      children: [{name: cur.service_user}] 
     } 
     ] 
    }); 
    } 
    return acc; 
}, []); 
関連する問題