将同一仓库的重复产品合并,得到总数量,按平均价格((总数量/100)* 总价)生成新价格
let inventories = [
{ product: 'laptop', price: 50, warehouse: 'Kismayu', quantity: 6 },
{ product: 'phone', price: 20, warehouse: 'Mogadishu', quantity: 3 },
{ product: 'notebook', price: 30, warehouse: 'Mogadishu', quantity: 2 },
{ product: 'phone', price: 40, warehouse: 'Mogadishu', quantity: 1 },
{ product: 'phone', price: 60, warehouse: 'Hargeisa', quantity: 3 },
]
最终输出:
[
{ product: 'laptop', price: 50, warehouse: 'Kismayu', quantity: 6 },
{ product: 'phone', price: 24, warehouse: 'Mogadishu', quantity: 4 },
{ product: 'notebook', price: 30, warehouse: 'Mogadishu', quantity: 2 },
{ product: 'phone', price: 60, warehouse: 'Hargeisa', quantity: 3 },
]
1条答案
按热度按时间shyt4zoc1#
我对'摩加迪沙'和'phone'的平均价格有点困惑。您期望的平均价格是24,而20和40的平均价格是30。假设30是正确的计算方法,下面是一个函数式编程解决方案:
输出: