Ali Ali - 22 days ago 14
reST (reStructuredText) Question

Aggregate nested data using Underscore Js

I have the following raw data, and I would like to aggregate it to return an object with total for each location, and perhaps a grand total too.

Location A would be 760

122 (costPrice) * 4 (quantity) = 488 (ALCATEL 5054)

136 * 2 = 272 (DESIRE 530)

Location B would be 300

104 * 2 = 208 (ALCATEL 6060)

92 * 1 = 92 (ALCATEL 7972)

Grand total: 1060

How can I perform the following data transformation using underscore js?

I started this plunker, but don't think I am going in the right direction...
http://plnkr.co/edit/ThvyQB3tm5KFuE6oLM1n?p=preview

Raw Data:

[{
"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]
}]


Possible Desire Result (could be different format, with same data and total):

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

Answer
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;
}, {});

Which sets results to:

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

(It's 624 instead of 760 because there's only one instance of "DESIRE 530" in the sample data provided in your question.)

There's also _.each() which can be used to do the same thing in your case:

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

Reference:

Comments