gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 05:39:00 25 4
gpt4 key购买 nike

我在 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com