如何将json数据与javascript组合在一起

时间:2012-04-06 22:26:11

标签: javascript json

我有一些json,我正在尝试将它分组。 我试图用javascript做到这一点。理想情况下,我希望将JSON组合在一起,只为每个组分配一个数字,以便我可以遍历它。 我的最终目标是拥有一个功能,以便我可以选择要分组的键。

我尝试过使用某个功能,但没有成功。我不知道从哪里开始。

var groupBy = function(array, predicate) {
    var grouped = {};
    for(var i = 0; i < array.length; i++) {
        var groupKey = predicate(array[i]);
        if (typeof(grouped[groupKey]) === "undefined")
            grouped[groupKey] = [];
        grouped[groupKey].push(array[i]);
    }

    return grouped;
}

var groupData = groupBy(data, function (obj) {
    console.log(obj);
     return obj.category;
 });

我的json是

{
  "merchantcontract":"My Angel Dream Day Care Center 1 10137.01",
  "smi_transactiondate":"\/Date(1332831600000)\/",
  "smi_glamount2":5.8700,
  "smi_transactionclass":180870001,
  "smi_transactionclassname":"Residual Agent Commission",
  "smi_contractprodcutidname":"Traditional",
  "smi_agentid":"1d3f44ee-afc3-e011-addf-a4badb1ddef5",
  "smi_primaryagentid":"1d3f44ee-afc3-e011-addf-a4badb1ddef5"
},

{
  "merchantcontract":"My Angel Dream Day Care Center 1 10137.01",
  "smi_transactiondate":"\/Date(1331622000000)\/",
  "smi_glamount2":4.5792,
  "smi_transactionclass":180870001,
  "smi_transactionclassname":"Residual Agent Commission",
  "smi_contractprodcutidname":"Traditional",
  "smi_agentid":"1d3f44ee-afc3-e011-addf-a4badb1ddef5",
  "smi_primaryagentid":"1d3f44ee-afc3-e011-addf-a4badb1ddef5"
},

{
  "merchantcontract":"Eds Overhead Doors 10150.01",
  "smi_transactiondate":"\/Date(1333695600000)\/",
  "smi_glamount2":11.9300,
  "smi_transactionclass":180870001,
  "smi_transactionclassname":"Residual Agent Commission",
  "smi_contractprodcutidname":"Traditional",
  "smi_agentid":"1d3f44ee-afc3-e011-addf-a4badb1ddef5",
  "smi_primaryagentid":"1d3f44ee-afc3-e011-addf-a4badb1ddef5"
},

我需要的是将所有相同的商家合同分组,并将所有残差分组,以便我可以将glamount2加起来

-My Angel Dream Day Care Center 1 10137.01
--Residual //This could be anything
----total of glamount2 in the residual of my angle
--foo
----total of glamount2 in the foo of my angle

1 个答案:

答案 0 :(得分:0)

也许我错过了一些东西(你的意图让我感到困惑)但是如果你只需要迭代这些对象并得到一个特定的属性,你就不需要以任何方式“重新组合”它们。

简单地遍历对象并获得所需的任何属性:

​for(var i=0; i < myjson.length; i++){
    console.log(myjson[i].smi_glamount2);
}​

这假设这3个(或更多)对象都包含在单个数组中,如下所示:

var myjson = [{
    "merchantcontract": "My Angel Dream Day Care Center 1 10137.01",
    "smi_transactiondate": "\/Date(1332831600000)\/",
    "smi_glamount2": 5.8700,
    "smi_transactionclass": 180870001,
    "smi_transactionclassname": "Residual Agent Commission",
    "smi_contractprodcutidname": "Traditional",
    "smi_agentid": "1d3f44ee-afc3-e011-addf-a4badb1ddef5",
    "smi_primaryagentid": "1d3f44ee-afc3-e011-addf-a4badb1ddef5"
},
{
    "merchantcontract": "My Angel Dream Day Care Center 1 10137.01",
    "smi_transactiondate": "\/Date(1331622000000)\/",
    "smi_glamount2": 4.5792,
    "smi_transactionclass": 180870001,
    "smi_transactionclassname": "Residual Agent Commission",
    "smi_contractprodcutidname": "Traditional",
    "smi_agentid": "1d3f44ee-afc3-e011-addf-a4badb1ddef5",
    "smi_primaryagentid": "1d3f44ee-afc3-e011-addf-a4badb1ddef5"
},
{
    "merchantcontract": "Eds Overhead Doors 10150.01",
    "smi_transactiondate": "\/Date(1333695600000)\/",
    "smi_glamount2": 11.9300,
    "smi_transactionclass": 180870001,
    "smi_transactionclassname": "Residual Agent Commission",
    "smi_contractprodcutidname": "Traditional",
    "smi_agentid": "1d3f44ee-afc3-e011-addf-a4badb1ddef5",
    "smi_primaryagentid": "1d3f44ee-afc3-e011-addf-a4badb1ddef5"
}];