使用 Underscore Js 聚合嵌套数据

Aggregate nested data using Underscore Js

我有以下原始数据,我想将其汇总到 return 一个对象,每个位置都有总计,也许还有总计。

位置 A 为 760

122(成本价)* 4(数量)= 488(阿尔卡特 5054)

136 * 2 = 272(期望 530)

位置 B 为 300

104 * 2 = 208(阿尔卡特 6060)

92 * 1 = 92 (阿尔卡特 7972)

总计:1060

如何使用underscore 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
}]
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,因为在您的问题中提供的示例数据中只有一个 "DESIRE 530" 实例。)

还有 _.each() 可以用来在你的情况下做同样的事情:

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

参考: