2016-11-10 99 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