2016-12-13 24 views
1

的重复计数分组值让我有看起来像这样的数据对象:与元件

[ 
    { 
    "scenario_id": 238, 
    "scenario_desc": "k1204", 
    "scenario_status": null, 
    "scn_appl_lob_ownr_nm": "Crd", 
    "scenario_asv_id": 21216, 
    "appl_ci_id": "CI256747", 
    "mth_dt": "2015-01", 
    "cost_curr": 888.7326, 
    "qty_curr": "2", 
    "cost_trgt": 0, 
    "qty_trgt": "0", 
    "ftprnt": [ 
     "Both" 
    ] 
    }, 
    { 
    "scenario_id": 238, 
    "scenario_desc": "k1204", 
    "scenario_status": null, 
    "scn_appl_lob_ownr_nm": "Crd", 
    "scenario_asv_id": 21216, 
    "appl_ci_id": "CI256747", 
    "mth_dt": "2015-01", 
    "cost_curr": 13479.6678574427, 
    "qty_curr": "17", 
    "cost_trgt": 0, 
    "qty_trgt": "0", 
    "ftprnt": [ 
     "Both" 
    ] 
    }, 
    { 
    "scenario_id": 238, 
    "scenario_desc": "k1204", 
    "scenario_status": null, 
    "scn_appl_lob_ownr_nm": "Crd", 
    "scenario_asv_id": 21216, 
    "appl_ci_id": "CI256747", 
    "mth_dt": "2015-01", 
    "cost_curr": 11295.401684737, 
    "qty_curr": "17", 
    "cost_trgt": 0, 
    "qty_trgt": "0", 
    "ftprnt": [ 
     "Both" 
    ] 
    }, 
    { 
    "scenario_id": 238, 
    "scenario_desc": "k1204", 
    "scenario_status": null, 
    "scn_appl_lob_ownr_nm": "Crd", 
    "scenario_asv_id": 21216, 
    "appl_ci_id": "CI256747", 
    "mth_dt": "2015-01", 
    "cost_curr": 2263.9328, 
    "qty_curr": "10", 
    "cost_trgt": 0, 
    "qty_trgt": "0", 
    "ftprnt": [ 
     "Both" 
    ] 
    }, 
    { 
    "scenario_id": 238, 
    "scenario_desc": "k1204", 
    "scenario_status": null, 
    "scn_appl_lob_ownr_nm": "Crd", 
    "scenario_asv_id": 21215, 
    "appl_ci_id": "CI291768", 
    "mth_dt": "2015-01", 
    "cost_curr": 58406.3066, 
    "qty_curr": "17", 
    "cost_trgt": 0, 
    "qty_trgt": "0", 
    "ftprnt": [ 
     "Cloud" 
    ] 
    }, 
    { 
    "scenario_id": 178, 
    "scenario_desc": "ktest", 
    "scenario_status": null, 
    "scn_appl_lob_ownr_nm": "Crd", 
    "scenario_asv_id": 11028, 
    "appl_ci_id": "ASV", 
    "mth_dt": "2015-01", 
    "cost_curr": 0, 
    "qty_curr": "0", 
    "cost_trgt": 0, 
    "qty_trgt": "0", 
    "ftprnt": [ 
     "Cloud" 
    ] 
    }, 
    { 
    "scenario_id": 178, 
    "scenario_desc": "ktest", 
    "scenario_status": null, 
    "scn_appl_lob_ownr_nm": "Crd", 
    "scenario_asv_id": 11028, 
    "appl_ci_id": "ASV", 
    "mth_dt": "2015-01", 
    "cost_curr": 0, 
    "qty_curr": "0", 
    "cost_trgt": 0, 
    "qty_trgt": "0", 
    "ftprnt": [ 
     "Cloud" 
    ] 
    }, 
    { 
    "scenario_id": 178, 
    "scenario_desc": "ktest", 
    "scenario_status": null, 
    "scn_appl_lob_ownr_nm": "Crd", 
    "scenario_asv_id": 11028, 
    "appl_ci_id": "ASV", 
    "mth_dt": "2015-01", 
    "cost_curr": 2259.994, 
    "qty_curr": "6", 
    "cost_trgt": 0, 
    "qty_trgt": "0", 
    "ftprnt": [ 
     "Cloud" 
    ] 
    }, 
    { 
    "scenario_id": 178, 
    "scenario_desc": "ktest", 
    "scenario_status": null, 
    "scn_appl_lob_ownr_nm": "Crd", 
    "scenario_asv_id": 11028, 
    "appl_ci_id": "ASV", 
    "mth_dt": "2015-01", 
    "cost_curr": 0, 
    "qty_curr": "0", 
    "cost_trgt": 0, 
    "qty_trgt": "0", 
    "ftprnt": [ 
     "Cloud" 
    ] 
    } 
] 

,现在,我可以使用D3巢汇总由scenario_desc返回成本(cost_curr + cost_trgt)的总和, ftprnt和mth_dt这样的:

var costByScn = d3.nest() 
    .key(function(d) { return d.scenario_desc }) 
    .key(function(d) { return d.ftprnt }) 
    .key(function(d) { 

     var mth_dt = new Date(d.mth_dt).getTime(); 
     return mth_dt; 
     }) 
    .rollup(function(v) { 

      return parseFloat(d3.sum(v, function(d) { return d.cost_curr + d.cost_trgt })) 

    }) 
    .entries(response); 
console.log("costByScn: ", JSON.stringify(costByScn)); 

,但我将如何得到appl_ci_id重复计数由scenario_desc,ftprnt和mth_dt?我还没有使用D3,我可以使用的东西,如lodash做太多同样的事情有限...

最终的结果是这样的:

[ 
    { 
    "key": "k1204", 
    "values": [ 
     { 
     "key": "Both", 
     "values": [ 
      { 
      "key": "1420070400000", 
      "values": 1 
      } 
     ] 
     }, 
     { 
     "key": "Cloud", 
     "values": [ 
      { 
      "key": "1420070400000", 
      "values": 1 
      } 
     ] 
     } 
    ] 
    }, 
    { 
    "key": "ktest", 
    "values": [ 
     { 
     "key": "Cloud", 
     "values": [ 
      { 
      "key": "1420070400000", 
      "values": 1 
      } 
     ] 
     } 
    ] 
    } 
] 

这里是代码jsbin以及...

https://jsbin.com/jicuxohawi/edit?js,console

回答

0

得到它的工作使用lodash ...

var costByScn = d3.nest() 
    .key(function(d) { return d.scenario_desc }) 
    .key(function(d) { return d.ftprnt }) 
    .key(function(d) { 

     var mth_dt = new Date(d.mth_dt).getTime(); 
     return mth_dt; 
     }) 
    .rollup(function(v) { 

      var z = _.map(v, 'appl_ci_id'); 
      console.log("z is: ", z) 

      var q = _.uniq(z) 

      console.log("q is: ", q) 
      var len = q.filter(function(val, i, arr) { 
          return arr.indexOf(val) === i; 
          }).length; 
      return len; 


    }) 
    .entries(response);