使用Underscore Js

时间:2016-11-10 02:44:07

标签: javascript arrays json rest underscore.js

我有以下原始数据,我希望将其聚合以返回每个位置的总计对象,也可能是总计。

地点A将是760

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

136 * 2 = 272(DESIRE 530)

位置B为300

104 * 2 = 208(ALCATEL 6060)

92 * 1 = 92(ALCATEL 7972)

总计:1060

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

我开始使用这个弹药,但不要以为我朝着正确的方向前进...... 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
}]

1 个答案:

答案 0 :(得分: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,因为在您的问题中提供的示例数据中只有一个“DESIRE 530”实例。)

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

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

参考: