gpt4 book ai didi

javascript - 从功能上总结集合

转载 作者:行者123 更新时间:2023-12-01 01:11:47 24 4
gpt4 key购买 nike

有人可以建议一种方法来实现这一功能吗?也许用reduce()?如果名称、重量和研磨程度相同,则将数量添加到汇总集合中。也许洛达什?

let bigOrder = [
{ name: "House Blend", quantity: 1, weight: "250g", grind: "whole-beans" },
{ name: "House Blend", quantity: 3, weight: "250g", grind: "filter" },
{ name: "Decaf", quantity: 1, weight: "250g", grind: "whole-beans" },
{ name: "Colombia", quantity: 4, weight: "250g", grind: "home-espresso" },
{ name: "Decaf", quantity: 1, weight: "250g", grind: "whole-beans" },
{ name: "House Blend", quantity: 2, weight: "250g", grind: "filter" },
{ name: "Colombia", quantity: 1, weight: "250g", grind: "filter" },
{ name: "Decaf", quantity: 1, weight: "250g", grind: "plunger" },
{ name: "Colombia", quantity: 3, weight: "1kg", grind: "whole-beans" },
{ name: "Colombia", quantity: 1, weight: "250g", grind: "whole-beans" },
{ name: "Decaf", quantity: 1, weight: "250g", grind: "plunger" },
{ name: "House Blend", quantity: 5, weight: "1kg", grind: "filter" },
{ name: "House Blend", quantity: 5, weight: "1kg", grind: "filter" },
{ name: "House Blend", quantity: 4, weight: "250g", grind: "filter" }
];

const summariseOrder = () => {
let summary = [];

bigOrder.forEach(line => {
function comparator(a, b) {
return a.name === b.name && a.weight === b.weight && a.grind === b.grind;
}

let index = summary.findIndex(e => comparator(e, line));
if (index > 0) {
summary[index].quantity += line.quantity;
} else {
summary.push(line);
}
});

return summary;
};

console.log(summariseOrder());

最佳答案

您可以通过根据名称、重量和研磨创建一个键来使用reduce - 如果该键已存在于累加器对象中,请添加到.quantity中已经存在的对象,最后获取该对象的值:

let bigOrder = [
{ name: "House Blend", quantity: 1, weight: "250g", grind: "whole-beans" },
{ name: "House Blend", quantity: 3, weight: "250g", grind: "filter" },
{ name: "Decaf", quantity: 1, weight: "250g", grind: "whole-beans" },
{ name: "Colombia", quantity: 4, weight: "250g", grind: "home-espresso" },
{ name: "Decaf", quantity: 1, weight: "250g", grind: "whole-beans" },
{ name: "House Blend", quantity: 2, weight: "250g", grind: "filter" },
{ name: "Colombia", quantity: 1, weight: "250g", grind: "filter" },
{ name: "Decaf", quantity: 1, weight: "250g", grind: "plunger" },
{ name: "Colombia", quantity: 3, weight: "1kg", grind: "whole-beans" },
{ name: "Colombia", quantity: 1, weight: "250g", grind: "whole-beans" },
{ name: "Decaf", quantity: 1, weight: "250g", grind: "plunger" },
{ name: "House Blend", quantity: 5, weight: "1kg", grind: "filter" },
{ name: "House Blend", quantity: 5, weight: "1kg", grind: "filter" },
{ name: "House Blend", quantity: 4, weight: "250g", grind: "filter" }
];

const summariseOrder = arr => Object.values(arr.reduce(
(a, obj) => {
const key = `${obj.name}_${obj.weight}_${obj.grind}`;
if (!a[key]) a[key] = obj;
else a[key].quantity += obj.quantity;
return a;
}, {}
));
console.log(summariseOrder(bigOrder));

关于javascript - 从功能上总结集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55095502/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com