如何分组(多键)和 javascript 数组中多个 属性 值的总和(NodeJs)
How to group by(multiple key) and sum of multiple property values in javascript array (NodeJs)
在javascript 数组
中按(多键)和多个属性 值的总和进行分组的最有效方法是什么
前-
[
{ Phase: "Phase 1", Step: "Step 1", Value1: "15", Value2: "5" },
{ Phase: "Phase 1", Step: "Step 2", Value1: "20", Value2: "10" },
{ Phase: "Phase 2", Step: "Step 1", Value1: "25", Value2: "15" },
{ Phase: "Phase 2", Step: "Step 2", Value1: "30", Value2: "20" },
{ Phase: "Phase 1", Step: "Step 1", Value1: "35", Value2: "25" },
{ Phase: "Phase 1", Step: "Step 2", Value1: "40", Value2: "30" },
{ Phase: "Phase 2", Step: "Step 1", Value1: "45", Value2: "35" },
{ Phase: "Phase 2", Step: "Step 2", Value1: "50", Value2: "40" }
]
预期输出
[
{ Phase: "Phase 1", Step: "Step 1", Value1: "50", Value2: "30" },
{ Phase: "Phase 1", Step: "Step 2", Value1: "60", Value2: "40" },
{ Phase: "Phase 2", Step: "Step 1", Value1: "70", Value2: "50" },
{ Phase: "Phase 2", Step: "Step 2", Value1: "80", Value2: "60" }
]
你可以用 reduce
做到这一点,并从中获取 Object.values
:
var array = [ { Phase: "Phase 1", Step: "Step 1", Value1: "15", Value2: "5" }, { Phase: "Phase 1", Step: "Step 2", Value1: "20", Value2: "10" }, { Phase: "Phase 2", Step: "Step 1", Value1: "25", Value2: "15" }, { Phase: "Phase 2", Step: "Step 2", Value1: "30", Value2: "20" }, { Phase: "Phase 1", Step: "Step 1", Value1: "35", Value2: "25" }, { Phase: "Phase 1", Step: "Step 2", Value1: "40", Value2: "30" }, { Phase: "Phase 2", Step: "Step 1", Value1: "45", Value2: "35" }, { Phase: "Phase 2", Step: "Step 2", Value1: "50", Value2: "40" }];
var result = Object.values(array.reduce((acc, {Value1, Value2,...rest})=>{
const key = Object.values(rest).join('|');
acc[key] = acc[key] || {...rest, Value1:0,Value2:0};
acc[key].Value1+=+Value1;
acc[key].Value2+=+Value2;
return acc;
},{}));
console.log(result);
const data = [{Phase:"Phase 1",Step:"Step 1",Value1:"15",Value2:"5"},{Phase:"Phase 1",Step:"Step 2",Value1:"20",Value2:"10"},{Phase:"Phase 2",Step:"Step 1",Value1:"25",Value2:"15"},{Phase:"Phase 2",Step:"Step 2",Value1:"30",Value2:"20"},{Phase:"Phase 1",Step:"Step 1",Value1:"35",Value2:"25"},{Phase:"Phase 1",Step:"Step 2",Value1:"40",Value2:"30"},{Phase:"Phase 2",Step:"Step 1",Value1:"45",Value2:"35"},{Phase:"Phase 2",Step:"Step 2",Value1:"50",Value2:"40"}]
const finalResult = data.reduce((result, d) => {
let key = `${d.Phase}_${d.Step}`;
result[key] = result[key] || {...d, Value1: 0, Value2: 0};
result[key].Value1 += +d.Value1;
result[key].Value2 += +d.Value2
return result
}, {})
console.log(Object.values(finalResult))
在javascript 数组
中按(多键)和多个属性 值的总和进行分组的最有效方法是什么前-
[
{ Phase: "Phase 1", Step: "Step 1", Value1: "15", Value2: "5" },
{ Phase: "Phase 1", Step: "Step 2", Value1: "20", Value2: "10" },
{ Phase: "Phase 2", Step: "Step 1", Value1: "25", Value2: "15" },
{ Phase: "Phase 2", Step: "Step 2", Value1: "30", Value2: "20" },
{ Phase: "Phase 1", Step: "Step 1", Value1: "35", Value2: "25" },
{ Phase: "Phase 1", Step: "Step 2", Value1: "40", Value2: "30" },
{ Phase: "Phase 2", Step: "Step 1", Value1: "45", Value2: "35" },
{ Phase: "Phase 2", Step: "Step 2", Value1: "50", Value2: "40" }
]
预期输出
[
{ Phase: "Phase 1", Step: "Step 1", Value1: "50", Value2: "30" },
{ Phase: "Phase 1", Step: "Step 2", Value1: "60", Value2: "40" },
{ Phase: "Phase 2", Step: "Step 1", Value1: "70", Value2: "50" },
{ Phase: "Phase 2", Step: "Step 2", Value1: "80", Value2: "60" }
]
你可以用 reduce
做到这一点,并从中获取 Object.values
:
var array = [ { Phase: "Phase 1", Step: "Step 1", Value1: "15", Value2: "5" }, { Phase: "Phase 1", Step: "Step 2", Value1: "20", Value2: "10" }, { Phase: "Phase 2", Step: "Step 1", Value1: "25", Value2: "15" }, { Phase: "Phase 2", Step: "Step 2", Value1: "30", Value2: "20" }, { Phase: "Phase 1", Step: "Step 1", Value1: "35", Value2: "25" }, { Phase: "Phase 1", Step: "Step 2", Value1: "40", Value2: "30" }, { Phase: "Phase 2", Step: "Step 1", Value1: "45", Value2: "35" }, { Phase: "Phase 2", Step: "Step 2", Value1: "50", Value2: "40" }];
var result = Object.values(array.reduce((acc, {Value1, Value2,...rest})=>{
const key = Object.values(rest).join('|');
acc[key] = acc[key] || {...rest, Value1:0,Value2:0};
acc[key].Value1+=+Value1;
acc[key].Value2+=+Value2;
return acc;
},{}));
console.log(result);
const data = [{Phase:"Phase 1",Step:"Step 1",Value1:"15",Value2:"5"},{Phase:"Phase 1",Step:"Step 2",Value1:"20",Value2:"10"},{Phase:"Phase 2",Step:"Step 1",Value1:"25",Value2:"15"},{Phase:"Phase 2",Step:"Step 2",Value1:"30",Value2:"20"},{Phase:"Phase 1",Step:"Step 1",Value1:"35",Value2:"25"},{Phase:"Phase 1",Step:"Step 2",Value1:"40",Value2:"30"},{Phase:"Phase 2",Step:"Step 1",Value1:"45",Value2:"35"},{Phase:"Phase 2",Step:"Step 2",Value1:"50",Value2:"40"}]
const finalResult = data.reduce((result, d) => {
let key = `${d.Phase}_${d.Step}`;
result[key] = result[key] || {...d, Value1: 0, Value2: 0};
result[key].Value1 += +d.Value1;
result[key].Value2 += +d.Value2
return result
}, {})
console.log(Object.values(finalResult))