javascript - 在 Jquery 中总结和分组 JSON 对象

标签 javascript jquery json grouping summary

我在 Jquery 中总结和分组我的对象时遇到问题,因为我想按国家/地区分组,然后根据子周期 4、5 和 6 对 col1 中的值进行总计。 col1、col2、col3 是动态的,但使用这个只是一个例子。

我需要的是获取存储在 country 变量中的国家/地区总数、存储在 columns 变量中的每个子周期的总计以及存储在 averages 变量中的总数。

这是我在 jsfiddle 中的代码:

http://jsfiddle.net/8vVK7/8/

我的目标:

var g_jsonData =

[
{
    "Country": "Spain",
    "Year": "2000",
    "Subperiod": "4",
    "col1": "75",
    "col2": "500",
    "col3": "200"
},
{
    "Country": "Spain",
    "Year": "2001",
    "Subperiod": "4",
    "col1": "50",
    "col2": "500",
    "col3": "300"
},
{
    "Country": "Spain",
    "Year": "2002",
    "Subperiod": "4",      
    "col1": "50",
    "col2": "200",
    "col3": "300"
},
{
    "Country": "Spain",
    "Year": "2003",
    "Subperiod": "4",      
    "col1": "",
    "col2": "200",
    "col3": "300"
},
{
    "Country": "Spain",
    "Year": "2005",
    "Subperiod": "5",
    "col1": "125",
    "col2": "500",
    "col3": "300"
},
{
    "Country": "Spain",
    "Year": "2012",
    "Subperiod": "6",
    "col1": "100.75",
    "col2": "500",
    "col3": "200"
},
{
    "Country": "Spain",
    "Year": "2013",
    "Subperiod": "6",
    "col1": "200",
    "col2": "500",
    "col3": "300"
},
{
    "Country": "France",
    "Year": "2000",
    "Subperiod": "4",    
    "col1": "100",
    "col2": "100",
    "col3": "300"
},
{
    "Country": "France",
    "Year": "2001",
    "Subperiod": "4",      
    "col1": "100",
    "col2": "100",
    "col3": "300"
},
{
    "Country": "France",
    "Year": "2002",
    "Subperiod": "4",      
    "col1": "100",
    "col2": "200",
    "col3": "300"
},
{
    "Country": "France",
    "Year": "2005",
    "Subperiod": "5",      
    "col1": "100",
    "col2": "200",
    "col3": "300"
},
{
    "Country": "France",
    "Year": "2012",
    "Subperiod": "6",      
    "col1": "100",
    "col2": "100",
    "col3": "300"
},
{
    "Country": "France",
    "Year": "2013",
    "Subperiod": "6",      
    "col1": "100",
    "col2": "100",
    "col3": "300"
}];

这是我想要得到的最终结果,这是基于对每个国家/地区每个子时期的 col1 的总结。

countries =    
{
   "Spain":{
      "4":175,
      "5":125,
      "6":300.75
  },    
   "France":{
      "4":300,
      "5":100,
      "6":200,
  }
}

子周期 4、5、6 的总计应显示为:

columns = 
{
    "4":475,
    "5":225,
    "6":500.75
} 

我还想获得每个国家/地区的总计数:

averages = 
{
   "Spain":{
      "4":3,
      "5":1,
      "6":2
  },    
   "France":{
      "4":3,
      "5":1,
      "6":2
  }
}   

我想尽量坚持我的代码。 谢谢,感谢您的帮助。

最佳答案

这应该可以解决问题:

g_jsonData.reduce(function(out, curr){
    // Make sure the current country exists on the output object.
    // Then add the current object's col1 to the current country's subperiod,
    //   checking to see if the current subperiod exists. Also, cast the `col1` to int `(+curr.col1)`
    out.countries[curr.Country] = out.countries[curr.Country] || {};
    out.countries[curr.Country][curr.Subperiod] = (out.countries[curr.Country][curr.Subperiod] || 0) + (+curr.col1);

    // And add it to the total, too.
    out.columns[curr.Subperiod] = (out.columns[curr.Subperiod] || 0) + (+curr.col1);

    // Count occurences
    out.count[curr.Country] = out.count[curr.Country] || {};
    out.count[curr.Country][curr.Subperiod] = (out.count[curr.Country][curr.Subperiod] || 0) + 1;
    return out;
},
{'columns':{},'countries':{},'count':{}}); // second parameter to `reduce` is a default object, in this case: `{'columns':{},'countries':{},'count':{}}}`

返回:

{
    "columns": {
        "4": 475,
        "5": 225,
        "6": 500.75
    },
    "countries": {
        "Spain": {
            "4": 175,
            "5": 125,
            "6": 300.75
        },
        "France": {
            "4": 300,
            "5": 100,
            "6": 200
        }
    },
    "count": {
        "Spain": {
            "4": 3,
            "5": 1,
            "6": 2
        },
        "France": {
            "4": 3,
            "5": 1,
            "6": 2
        }
    }
}

请注意,这不会更改 g_jsonData,它只会返回新对象。
要使用新对象,您必须将其分配给一个变量,当然:

var output = g_jsonData.reduce(func....);

您可以像这样使代码更短一些(/更易于查看):

g_jsonData.reduce(function(out, curr){
    var c = curr.Country,
        s = curr.Subperiod,
        v = +curr.col1; //value cast to int already
    out.countries[c]    =  out.countries[c]    || {};
    out.countries[c][s] = (out.countries[c][s] || 0 ) + v;
    out.count[c]        =  out.count[c]        || {};
    out.count[c][s]     = (out.count[c][s]     || 0 ) + 1;
    out.columns[s]      = (out.columns[s]      || 0 ) + v;
    return out;
},
{'columns':{},'countries':{},'count':{}});

现在让 col1 成为一个变量名就相对容易了。
替换:

v = +curr.col1;

与:

v = +curr['col1'];

或者:

v = +curr[someVariable];

关于javascript - 在 Jquery 中总结和分组 JSON 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21874436/

相关文章:

javascript - 如何获取元素周围的文本?

javascript - 开发工具 - 类型错误 : 'stepUp'

jQuery:在选择框中快速选择整个 optgroup 的简单方法

javascript - 具有嵌套数据的 Material UI 数据网格

java - 如何在 ListView 中显示 arraylist 中的所有解析数据?

javascript - 寻找数组的众数? JavaScript

javascript - Firebase/Firestore : No document exists but it should

javascript - 当 Router.route(uri, {name : templateName })

javascript - 从不同域动态同步加载 JavaScript 文件

android - Retrofit 2,同名不同数据类型JSON解析