有很多关于格式化JSON的帖子,但我找不到适合我奇怪情况的帖子......这是我的数据源:
data = [{ "LoanOfficer": "Brett", "Year": 2014, "Month": 10, "FundedVolume": 304032.0000, "FundedUnits": 2.0000, "NewAppUnits": 2.0000, "RateLockUnits": 1.0000 },
{ "LoanOfficer": "Brett", "Year": 2014, "Month": 11, "FundedVolume": 179450.0000, "FundedUnits": 1.0000, "NewAppUnits": 1.0000, "RateLockUnits": 3.0000 },
{ "LoanOfficer": "Carlos", "Year": 2014, "Month": 10, "FundedVolume": 576615.0000, "FundedUnits": 3.0000, "NewAppUnits": 5.0000, "RateLockUnits": 3.0000 },
{ "LoanOfficer": "Carlos", "Year": 2014, "Month": 11, "FundedVolume": 341303.0000, "FundedUnits": 2.0000, "NewAppUnits": 5.0000, "RateLockUnits": null },
{ "LoanOfficer": "Carlos", "Year": 2014, "Month": 12, "FundedVolume": null, "FundedUnits": null, "NewAppUnits": null, "RateLockUnits": 1.0000 },
{ "LoanOfficer": "Elaine", "Year": 2014, "Month": 10, "FundedVolume": null, "FundedUnits": null, "NewAppUnits": 1.0000, "RateLockUnits": 1.0000 },
{ "LoanOfficer": "Elaine", "Year": 2014, "Month": 11, "FundedVolume": 348500.0000, "FundedUnits": 2.0000, "NewAppUnits": 1.0000, "RateLockUnits": 1.0000 },
{ "LoanOfficer": "Eric", "Year": 2014, "Month": 10, "FundedVolume": 768464.0000, "FundedUnits": 6.0000, "NewAppUnits": 5.0000, "RateLockUnits": 7.0000 }];
因此,您可以看到我有相同LoanOfficer
的多个条目。理想情况下,我想重新格式化并总结,以便每LoanOfficer
只有一个条目并添加FundedVolume
& *Units
对此:
data = [{ "LoanOfficer": "Brett", "FundedVolume": 483482.0000, "FundedUnits": 3.0000, "NewAppUnits": 3.0000, "RateLockUnits": 4.0000 },
{ "LoanOfficer": "Carlos", "FundedVolume": 917918.0000, "FundedUnits": 5.0000, "NewAppUnits": 10.0000, "RateLockUnits": 4.0000 },
{ "LoanOfficer": "Elaine", "FundedVolume": 348500.0000, "FundedUnits": 2.0000, "NewAppUnits": 2.0000, "RateLockUnits": 2.0000 },
{ "LoanOfficer": "Eric", "FundedVolume": 768464.0000, "FundedUnits": 6.0000, "NewAppUnits": 5.0000, "RateLockUnits": 7.0000 }];
我试过这个作为起点,但我很确定这不是正确的路线。
var UniqueD = {};
$.each(data, function () {
i = 0;
if (UniqueD[this.LoanOfficer])
i = UniqueD[this.LoanOfficer];
UniqueD[this.LoanOfficer] = i + this.NewAppUnits;
});
编辑:我有一个工作功能,可以提供我想要的东西:
var rawdata = [...] // data source
var listOfOfficerStats = []; // new data
function Combine() {
for (i = 0; i < rawData.length; i++)
{
var loExist = -1;
for (j = 0; j < listOfOfficerStats.length; j++)
{
if (listOfOfficerStats[j].LoanOfficer == rawData[i].LoanOfficer)
loExist = j;
}
if (loExist == -1)
{
//New
var newObject = new Object();
newObject.LoanOfficer = rawData[i].LoanOfficer;
newObject.FundedVolume = rawData[i].FundedVolume;
newObject.FundedUnits = rawData[i].FundedUnits;
newObject.NewAppVolume = rawData[i].NewAppVolume;
newObject.NewAppUnits = rawData[i].NewAppUnits;
newObject.RateLockVolume = rawData[i].RateLockVolume;
newObject.RateLockUnits = rawData[i].RateLockUnits;
listOfOfficerStats.push(newObject);
}
else
{
//Add To Existing
listOfOfficerStats[loExist].FundedVolume += rawData[i].FundedVolume;
listOfOfficerStats[loExist].FundedUnits += rawData[i].FundedUnits;
listOfOfficerStats[loExist].NewAppVolume += rawData[i].NewAppVolume;
listOfOfficerStats[loExist].NewAppUnits += rawData[i].NewAppUnits;
listOfOfficerStats[loExist].RateLockVolume += rawData[i].RateLockVolume;
listOfOfficerStats[loExist].RateLockUnits += rawData[i].RateLockUnits;
}
}
}
答案 0 :(得分:1)
这是一个通用函数:
function sum(data, key, fields) {
return data.reduce(function(out, rec) {
var k = rec[key];
out[k] = out[k] || {};
fields.forEach(function(f) {
out[k][f] = (out[k][f] || 0) + (rec[f] || 0);
});
return out;
}, {})
}
// test
data = [{ "LoanOfficer": "Brett", "Year": 2014, "Month": 10, "FundedVolume": 304032.0000, "FundedUnits": 2.0000, "NewAppUnits": 2.0000, "RateLockUnits": 1.0000 },
{ "LoanOfficer": "Brett", "Year": 2014, "Month": 11, "FundedVolume": 179450.0000, "FundedUnits": 1.0000, "NewAppUnits": 1.0000, "RateLockUnits": 3.0000 },
{ "LoanOfficer": "Carlos", "Year": 2014, "Month": 10, "FundedVolume": 576615.0000, "FundedUnits": 3.0000, "NewAppUnits": 5.0000, "RateLockUnits": 3.0000 },
{ "LoanOfficer": "Carlos", "Year": 2014, "Month": 11, "FundedVolume": 341303.0000, "FundedUnits": 2.0000, "NewAppUnits": 5.0000, "RateLockUnits": null },
{ "LoanOfficer": "Carlos", "Year": 2014, "Month": 12, "FundedVolume": null, "FundedUnits": null, "NewAppUnits": null, "RateLockUnits": 1.0000 },
{ "LoanOfficer": "Elaine", "Year": 2014, "Month": 10, "FundedVolume": null, "FundedUnits": null, "NewAppUnits": 1.0000, "RateLockUnits": 1.0000 },
{ "LoanOfficer": "Elaine", "Year": 2014, "Month": 11, "FundedVolume": 348500.0000, "FundedUnits": 2.0000, "NewAppUnits": 1.0000, "RateLockUnits": 1.0000 },
{ "LoanOfficer": "Eric", "Year": 2014, "Month": 10, "FundedVolume": 768464.0000, "FundedUnits": 6.0000, "NewAppUnits": 5.0000, "RateLockUnits": 7.0000 }];
res = sum(data, "LoanOfficer", ["FundedVolume", "FundedUnits"]);
document.write(JSON.stringify(res));
答案 1 :(得分:1)
Here's a version使用lodash和一系列groupBy,map和reduce:
var data = [...]; //assuming your original data array here
function sum(total, n) {
return total + n;
}
function sumProperty(data, prop) {
return _.reduce(_.pluck(data, prop), sum);
}
var groups = _.groupBy(data, 'LoanOfficer');
var reduced = _.map(groups, function(group, groupName) {
return {
LoanOfficer: groupName,
FundedVolume: sumProperty(group, 'FundedVolume'),
NewAppUnits: sumProperty(group, 'NewAppUnits'),
RateLockUnits: sumProperty(group, 'RateLockUnits')
};
});
这绝不是最佳选择。使用一些更专业的代码,你可以通过仅data
循环一次来进行分组和同时构建所有总和来改善这一点。