![](/img/trans.png)
[英]How to merge and get sum of the values two objects has same keys as objects
[英]Sum values of objects with same keys
我有一個像這樣的數據結構。 它基本上是一個對象數組。
const dataset = [
{
a: { total: 2, name: "red" },
b: { total: 3, name: "gold" },
c: { total: 6, name: "rose" },
},
{
a: { total: 10, name: "red" },
b: { total: 7, name: "purple" },
},
{
a: { total: 1, name: "pink" },
b: { total: 14, name: "blue" },
c: { total: 10, name: "rose" },
},
{
b: { total: 2, name: "green" },
c: { total: 18, name: "rose" },
},
]
這是我想計算的結果:
const result = {
a: 13,
b: 26,
c: 34,
}
因此,我需要具有相同鍵的對象的total
。我嘗試使用前面的示例來解釋自己:
const result = {
a: 13, // 2 + 10 + 1
b: 26, // 3 + 7 + 14 + 2
c: 34, // 6 + 10 + 18
}
請注意,鍵a
、 b
、 c
可能很多(不僅是 3 個)而且我不知道它們的名稱,因為它們是動態的。
我怎樣才能做到這一點? 我想按鍵對對象{total, name}
進行分組,然后按total
求和,但如何? 有可以幫助我的 Lodash 功能嗎?
這很簡單,遍歷數據集,然后遍歷數據集行中的所有鍵:
const dataset = [ { a: { total: 2, name: "red" }, b: { total: 3, name: "gold" }, c: { total: 6, name: "rose" } }, { a: { total: 10, name: "red" }, b: { total: 7, name: "purple" } }, { a: { total: 1, name: "pink" }, b: { total: 14, name: "blue" }, c: { total: 10, name: "rose" } }, { b: { total: 2, name: "green" }, c: { total: 18, name: "rose" } }, ]; const result = {}; dataset.forEach(row => { for (let key in row) { if (!result[key]) result[key] = 0; result[key] += row[key].total; } }); console.log(result);
const dataset = [{a: { total: 2, name: "red" }, b: { total: 3, name: "gold" }, c: { total: 6, name: "rose" }, }, {a: { total: 10, name: "red" }, b: { total: 7, name: "purple" }, }, {a: { total: 1, name: "pink" }, b: { total: 14, name: "blue" }, c: { total: 10, name: "rose" }, }, {b: { total: 2, name: "green" }, c: { total: 18, name: "rose" }, }, ]; const result = dataset.reduce((res, cur) => { for (const [key, value] of Object.entries(cur)) { res[key] = (res[key] || 0) + value.total; } return res; }, {}); console.log(result);
您可以使用數組 reduce :
const dataset = [ { a: { total: 2, name: "red" }, b: { total: 3, name: "gold" }, c: { total: 6, name: "rose" } }, { a: { total: 10, name: "red" }, b: { total: 7, name: "purple" } }, { a: { total: 1, name: "pink" }, b: { total: 14, name: "blue" }, c: { total: 10, name: "rose" } }, { b: { total: 2, name: "green" }, c: { total: 18, name: "rose" } } ]; function getCount() { return dataset.reduce((countObj, nextVal) => { Object.keys(nextVal).forEach(key => { if(countObj[key]){ countObj[key] += nextVal[key].total; }else{ countObj[key] = nextVal[key].total; } }); return countObj; }, {}); } console.log(getCount());
主意
構建一個總計字典,在您第一次遇到屬性時添加條目。
執行
迭代數據集中的項目,為每個項目迭代對象屬性。
假設:每個 item 對象都有一個屬性total
持有一個數字。
function tally ( pa_dataset ) { let dict_totals = {} ; pa_dataset.forEach ( po_record => { Object.entries(po_record).forEach ( pa_entry => { let s_key = pa_entry[0] , x_value = pa_entry[1] ; if (!dict_totals.hasOwnProperty(s_key)) { dict_totals[s_key] = 0; } dict_totals[s_key] += x_value.total; }); }); return dict_totals; } // tally // Test const dataset = [ { a: { total: 2, name: "red" }, b: { total: 3, name: "gold" }, c: { total: 6, name: "rose" }, }, { a: { total: 10, name: "red" }, b: { total: 7, name: "purple" }, }, { a: { total: 1, name: "pink" }, b: { total: 14, name: "blue" }, c: { total: 10, name: "rose" }, }, { b: { total: 2, name: "green" }, c: { total: 18, name: "rose" }, }, ]; console.log ( 'Totals:' ); console.log ( tally(dataset));
評論
沒有專門實現所需功能的 lodash 函數。
const map = new Map()
dataset.forEach(obj=>{
Object.keys(obj).forEach(key=>{
const value = obj[key]
if(value?.total){
if(!map.has(key)){
map.set(key,0)
}
map.set(key,map.get(key)+value.total)
}
})
})
map.forEach((v,k)=>{
console.log(`${k}:${v}`)
})
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.