87,993
社区成员
发帖
与我相关
我的任务
分享
var data = [
{
NO: 1,
YearMonth: 201706,
Area: '上海',
CustomerID: '100101',
Serial: 'KMC',
Count: 10
},
{
NO: 2,
YearMonth: 201706,
Area: '上海',
CustomerID: '100101',
Serial: 'KMG',
Count: 2
},
{
NO: 3,
YearMonth: 201706,
Area: '上海',
CustomerID: '100102',
Serial: 'KMG',
Count: 2
},
{
NO: 4,
YearMonth: 201706,
Area: '上海',
CustomerID: '100102',
Serial: 'MKF',
Count: 5
},
{
NO: 5,
YearMonth: 201706,
Area: '北京',
CustomerID: '100101',
Serial: 'MKF',
Count: 1
},
{
NO: 6,
YearMonth: 201706,
Area: '北京',
CustomerID: '100101',
Serial: 'MAX',
Count: 4
},
{
NO: 7,
YearMonth: 201706,
Area: '北京',
CustomerID: '100103',
Serial: 'XKM',
Count: 4
},
{
NO: 8,
YearMonth: 201706,
Area: '上海',
CustomerID: '100101',
Serial: 'KMC',
Count: 2
}
];
var area = {};//按地区存储
data.forEach(function (item) {
if (!area[item.Area]) {
area[item.Area] = [];
}
area[item.Area].push(item);
});
var o = {};//按消费者存储
for (var p in area) {
area[p].forEach(function (item) {
if(!o[item.CustomerID+"_"+item.Area]){
o[item.CustomerID+"_"+item.Area] = [];
}
o[item.CustomerID+"_"+item.Area].push(item);
});
}
//console.log(o)
var result = [];//最终结果
for(var k in o){
var a = {KMC:0,KMG:0,MKF:0,MAX:0,XKM:0};
o[k].forEach(function(ob){
if(!a[ob.Serial]){
a[ob.Serial] = ob.Count;
}else{
a[ob.Serial] += ob.Count;
}
a.YearMonth = ob.YearMonth;
a.Area = ob.Area;
a.CustomerID = ob.CustomerID;
});
result.push(a);
}
console.log(result);