2016-11-10 97 views
0

我有以下原始数据,并且我想汇总它以返回每个位置的总数,也可能是总计。使用Underscore汇总嵌套数据Js

位置A将是760

122(costPrice)* 4(数量)= 488(ALCATEL 5054)

136 * 2 = 272(DESIRE 530)

位置乙将300

104 * 2 = 208(ALCATEL 6060)

92 * 1 = 92(ALCATEL 7972)

总计:1060

如何可以执行使用下划线JS以下数据转换?

我开始这个plunker,但别以为我是在正确的方向前进...... http://plnkr.co/edit/ThvyQB3tm5KFuE6oLM1n?p=preview

原始数据:

[{ 
    "location": { 
     "id": 82008938, 
     "name": "LOCATION A", 
     "phone": "", 
     "address": "" 
    }, 
    "model": { 
     "id": 610214643353, 
     "name": "ALCATEL 5054", 
     "costPrice": 122, 
     "wholesalePrice": 127 
    }, 
    "order": "5698", 
    "sim": [358848659378096] 
}, { 
    "location": { 
     "id": 82009723, 
     "name": "LOCATION B", 
     "phone": "", 
     "address": "" 
    }, 
    "model": { 
     "id": 610214648136, 
     "name": "ALCATEL 6060", 
     "costPrice": 104, 
     "wholesalePrice": 120 
    }, 
    "order": "5698", 
    "sim": [358899043576662] 
}, { 
    "location": { 
     "id": 82008938, 
     "name": "LOCATION A", 
     "phone": "", 
     "address": "" 
    }, 
    "model": { 
     "id": 610214643353, 
     "name": "ALCATEL 5054", 
     "costPrice": 122, 
     "wholesalePrice": 127 
    }, 
    "order": "5698", 
    "sim": [358885796982333] 
}, { 
    "location": { 
     "id": 82009723, 
     "name": "LOCATION B", 
     "phone": "", 
     "address": "" 
    }, 
    "model": { 
     "id": 610214648136, 
     "name": "ALCATEL 6060", 
     "costPrice": 104, 
     "wholesalePrice": 120 
    }, 
    "order": "5698", 
    "sim": [358817108459730] 
}, { 
    "location": { 
     "id": 82008938, 
     "name": "LOCATION A", 
     "phone": "", 
     "address": "" 
    }, 
    "model": { 
     "id": 610214643353, 
     "name": "ALCATEL 5054", 
     "costPrice": 122, 
     "wholesalePrice": 127 
    }, 
    "order": "5698", 
    "sim": [358879619289409] 
}, { 
    "location": { 
     "id": 82008938, 
     "name": "LOCATION A", 
     "phone": "", 
     "address": "" 
    }, 
    "model": { 
     "id": 610214643353, 
     "name": "ALCATEL 5054", 
     "costPrice": 122, 
     "wholesalePrice": 127 
    }, 
    "order": "5698", 
    "sim": [358842400527891] 
}, { 
    "location": { 
     "id": 82009723, 
     "name": "LOCATION B", 
     "phone": "", 
     "address": "" 
    }, 
    "model": { 
     "id": 610214647597, 
     "name": "ALCATEL 7972", 
     "costPrice": 92, 
     "wholesalePrice": 95 
    }, 
    "order": "5709", 
    "sim": [358842726462666] 
}, { 
    "location": { 
     "id": 82008938, 
     "name": "LOCATION A", 
     "phone": "", 
     "address": "" 
    }, 
    "model": { 
     "id": 610214646606, 
     "name": "DESIRE 530", 
     "costPrice": 136, 
     "wholesalePrice": 149 
    }, 
    "order": "5710", 
    "sim": [358840719743714, 358848337490208] 
}] 

可能欲望的结果(可以是不同的格式,相同的数据和总数):

[{ 
    "location": "LOCATION A", 
    "total": 760 
}, { 
    "location": "LOCATION B", 
    "total": 300 
}] 

回答

0
var data = [ /* raw data */ ]; 

var results = _.reduce(data, function(results, item){ 
    results[item.location.name] = (results[item.location.name]|0) + item.model.costPrice; 
    return results; 
}, {}); 

其设定results到:

{ 
    "LOCATION A": 624, 
    "LOCATION B": 300 
} 

(这是624而不是760,因为有在你的问题中提供的样本数据“欲望530”只有一个实例。)

还有​​可以用来做你的情况同样的事情:

var results = {}; 
_.each(data, function(item){ 
    results[item.location.name] = (results[item.location.name]|0) + item.model.costPrice; 
}); 

参考:

+0

谢谢你的答复。虽然只有一个“DESIRE 530”实例,但价格是通过sim的长度(在这种情况下是2)来计算的,所以它将是136 * 2 = 272.对不起,如果我没有在问题中说清楚。但是我能够通过item.sim.length乘以costPrice来获得我所需要的。谢谢! – Ali