我有一個像這樣的資料結構。它基本上是一個物件陣列。
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 功能嗎?
uj5u.com熱心網友回復:
這很簡單,遍歷資料集,然后遍歷資料集行中的所有鍵:
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);
uj5u.com熱心網友回復:
您可以使用陣列 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());
uj5u.com熱心網友回復:
為什么不使用reducewith for of:
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);
uj5u.com熱心網友回復:
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}`)
})
uj5u.com熱心網友回復:
主意
構建一個總計字典,在您第一次遇到屬性時添加條目。
執行
迭代資料集中的專案,為每個專案迭代物件屬性。
假設:每個專案物件都有一個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;
});
});
console.log ( `Totals: ${JSON.stringify(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" },
},
];
tally(dataset);
評論
沒有專門實作所需功能的 lodash 函式。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/494305.html
標籤:javascript
上一篇:如何使用EigenC 轉換回圈矩陣乘法:不正確的結果
下一篇:Dog.prototype=Object.create(Animal.prototype)和Dog.prototype={...Animal.prototype}的結果有什么區別?
