我正在使用这个减少,因为我不仅需要计算类别,还需要计算级别。因此,我将有一个对象数组,其中包括每个类别和每个级别的主单元和子单元的计数例如
{
category: 1,
mainUnits: 14,
subUnits: 15,
levelA: {
mainUnits: 4,
subUnits: 6
},
levelB: {
mainUnits: 14,
subUnits: 15
}
}
const input = [
{
category: 1,
level: 'B',
mainUnits: 5,
subUnits: 3,
},
{
category: 1,
level: 'B',
mainUnits: 9,
subUnits: 12,
},
{
category: 2,
level: 'A',
mainUnits: 4,
subUnits: 6,
},
{
category: 2,
level: 'B',
mainUnits: 2,
subUnits: 4,
},
];
const result = input.reduce((result, item) => {
const existing = result.find((x) => x.category === item.category);
if (existing) {
existing.mainUnits += item.mainUnits;
existing.subUnits += item.subUnits;
} else {
result.push(item);
}
return result;
}, []);
console.log(result);
您可以对对象进行分组并获得结果值。内部检查级别,并添加一个属性为零值的对象。
然后为外部和嵌套级别添加单元。
const
input = [{ category: 1, level: 'B', mainUnits: 5, subUnits: 3 }, { category: 1, level: 'B', mainUnits: 9, subUnits: 12 }, { category: 2, level: 'A', mainUnits: 4, subUnits: 6 }, { category: 2, level: 'B', mainUnits: 2, subUnits: 4 }],
result = Object.values(input.reduce((r, { category, level, ...o }) => {
const
addTo = object => key => object[key] = (object[key] || 0) + o[key],
keys = ['mainUnits', 'subUnits'];
r[category] ??= { category};
keys.forEach(addTo(r[category]));
keys.forEach(addTo(r[category]['level' + level] ??= {}));
return r;
}, {}));
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
使用groupBy
,sumBy
和mapValues
的Lodash方法:
const input = [
{
category: 1,
level: 'B',
mainUnits: 5,
subUnits: 3,
},
{
category: 1,
level: 'B',
mainUnits: 9,
subUnits: 12,
},
{
category: 2,
level: 'A',
mainUnits: 4,
subUnits: 6,
},
{
category: 2,
level: 'B',
mainUnits: 2,
subUnits: 4,
},
];
const result = _(input)
.groupBy('category')
.map((items, category) => ({
category: parseInt(category),
mainUnits: _.sumBy(items, 'mainUnits'),
subUnits: _.sumBy(items, 'subUnits'),
..._.mapValues(_.groupBy(items, 'level'), (levelItems) => ({
mainUnits: _.sumBy(levelItems, 'mainUnits'),
subUnits: _.sumBy(levelItems, 'subUnits'),
})),
}))
.value();
console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.21/lodash.min.js"></script>
我在您的工作片段中添加了几行代码,它完成了工作。如果我错过了什么,请告诉我。
const input = [
{
category: 1,
level: "B",
mainUnits: 5,
subUnits: 3,
},
{
category: 1,
level: "B",
mainUnits: 9,
subUnits: 12,
},
{
category: 2,
level: "A",
mainUnits: 4,
subUnits: 6,
},
{
category: 2,
level: "B",
mainUnits: 2,
subUnits: 4,
},
];
const result = input.reduce((result, item) => {
const existing = result.find((x) => x.category === item.category);
if (existing) {
existing.mainUnits += item.mainUnits;
existing.subUnits += item.subUnits;
if (existing["level" + item.level]) {
existing["level" + item.level]["mainUnits"] += item.mainUnits;
existing["level" + item.level]["subUnits"] += item.subUnits;
} else {
existing["level" + item.level] = {
mainUnits: item.mainUnits,
subUnits: item.subUnits,
};
}
} else {
const { category, level, mainUnits, subUnits } = item;
const obj = {
category,
mainUnits,
subUnits,
["level" + level]: {
mainUnits,
subUnits,
},
};
result.push(obj);
}
return result;
}, []);
console.log(result);