我的数据结构是
companies = [
{name: 'A', ramps: [1, 2]},
{name: 'B', ramps: [3, 4, 5]}
...
]
ramps = [
{id: 1, division: 'accounting', amount: 500},
{id: 2, division: 'sale', amount: 200},
...
]
我的目标是最终获得以下数据:
groupedByDivision = [
{division: accounting, total: 12000},
{division: sales, total: 6500},
..
]
我的第一个蛮力方法是这样的(注意:我用这个flatten方法扩展了Ember.Array:
var ramps = this.get('controller.companies') // this is the array of companies
.get('model')
.mapProperty('ramps')
.flatten();
var temp = {}, data = [];
$.each(ramps, function(index, ramp) {
if ( !(ramp.billingDiv in tempCompanies) ) {
temp[ramp.billingDiv] = ramp.feeChange;
} else {
temp[ramp.billingDiv] += ramp.feeChange;
}
});
$.each(temp, function(division, amount) {
data.push({
'category': division,
'count': amount
});
});
return data;
还有其他建议吗?我对Ember.Array方法还不太熟悉,所以我不知道他们的所有用例。