我的数据结构是
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方法,所以我不了解它们的所有用例。