使用不同的元素计数对值进行分组

时间:2016-12-13 19:00:49

标签: javascript

让我有一个如下所示的数据对象:

[
  {
    "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 nest rollup返回由scenario_desc,ftprnt和mth_dt组成的成本(cost_curr + cost_trgt)的总和,如下所示:

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));   

但是如何通过scenario_desc,ftprnt和mth_dt获得appl_ci_id的独特计数?我也不限于使用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

1 个答案:

答案 0 :(得分:0)

使用lodash工作......

String input = "http://www.wolframalpha.com/input/?i=103%2F30+%3D+4a-3b,+71%2F60+%3D+a+%2B+b
,http://www.wolframalpha.com/input/?i=x%5E2%2B5x%2B6"

List<String> split = input.split('http');
List<String> finalList = new ArrayList<String>();

for(String fixup in split)
{
  finalList.put( "http" + fixup );
}
相关问题