gpt4 book ai didi

javascript - 在不改变原始数组的情况下将数组中的对象相加的算法

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:39:44 24 4
gpt4 key购买 nike

我有一系列待处理的付款作为对象,我想知道所有付款的总和,因为它们是针对同一家商店的。当函数被调用时,一旦它计算正确,当我再次调用它时,它会改变原始对象数组。我不明白为什么当我映射它时它会发生变异。

我需要不改变原始对象数组的函数。它应该进行计算并给我结果。如果再次调用,它也不应添加当前总和。它应该从头开始。

let pending = [ 
{Date: "20/12/2018",
Company:[
{Name: "Asda", Amount: 5.5},
{Name: "M&S", Amount: 10},
{Name: "Nisa", Amount: 15},
{Name: "Iceland", Amount: 10},
{Name: "Tesco", Amount: 5}
]},
{Date: "20/12/2018",
Company:[
{Name: "Asda", Amount: 5.5},
{Name: "M&S", Amount: 10},
{Name: "Nisa", Amount: 15},
{Name: "Iceland", Amount: 10},
{Name: "Tesco", Amount: 5}
]},
{Date: "20/12/2018",
Company:[
{Name: "Asda", Amount: 5.5},
{Name: "M&S", Amount: 10},
{Name: "Nisa", Amount: 15},
{Name: "Iceland", Amount: 10},
{Name: "Tesco", Amount: 5}
]},
{Date: "20/12/2018",
Company:[
{Name: "Asda", Amount: 5.5},
{Name: "M&S", Amount: 10},
{Name: "Nisa", Amount: 15},
{Name: "Iceland", Amount: 10},
{Name: "Tesco", Amount: 5}
]}
]

function returnSpendTotals() {
let sumSpend = []
let spendArray = pending.map(activities => activities.Company)
spendArray.flat().forEach(spend => {
let shopName = sumSpend.find(item => item.Name === spend.Name)
if (shopName) {
shopName.Amount += spend.Amount
} else {
sumSpend.push(spend)
}
})
return sumSpend
}

它应该在我每次调用 returnSpendTotals() 时返回

[{Name: "Asda", Amount: 22},
{Name: "M&S", Amount: 40},
{Name: "Nisa", Amount: 60},
{Name: "Iceland", Amount: 40},
{Name: "Tesco", Amount: 20}]

但是如果我第二次调用它,这就是我得到的结果

[{Name: "Asda", Amount: 38.5},
{Name: "M&S", Amount: 70},
{Name: "Nisa", Amount: 105},
{Name: "Iceland", Amount: 70},
{Name: "Tesco", Amount: 35}]

挂起的第一个对象现在是

{Company: [
{Name: "Asda", Amount: 38.5},
{Name: "M&S", Amount: 70},
{Name: "Nisa", Amount: 105},
{Name: "Iceland", Amount: 70},
{Name: "Tesco", Amount: 35}],
Date: "20/12/2018"}

其余pending中的对象不变

最佳答案

您可以采用解构的属性并为结果集构建一个新对象,以防止相同的对象引用所使用的数据。

function returnSpendTotals() {
return pending.reduce((r, { Company }) => {
Company.forEach(({ Name, Amount }) => {
let shop = r.find(item => item.Name === Name)
if (shop) {
shop.Amount += Amount;
} else {
r.push({ Name, Amount });
}
});
return r;
}, []);
}

let pending = [{ Date: "20/12/2018", Company: [{ Name: "Asda", Amount: 5.5 }, { Name: "M&S", Amount: 10 }, { Name: "Nisa", Amount: 15 }, { Name: "Iceland", Amount: 10 }, { Name: "Tesco", Amount: 5 }] }, { Date: "20/12/2018", Company: [{ Name: "Asda", Amount: 5.5 }, { Name: "M&S", Amount: 10 }, { Name: "Nisa", Amount: 15 }, { Name: "Iceland", Amount: 10 }, { Name: "Tesco", Amount: 5 }] }, { Date: "20/12/2018", Company: [{ Name: "Asda", Amount: 5.5 }, { Name: "M&S", Amount: 10 }, { Name: "Nisa", Amount: 15 }, { Name: "Iceland", Amount: 10 }, { Name: "Tesco", Amount: 5 }] }, { Date: "20/12/2018", Company: [{ Name: "Asda", Amount: 5.5 }, { Name: "M&S", Amount: 10 }, { Name: "Nisa", Amount: 15 }, { Name: "Iceland", Amount: 10 }, { Name: "Tesco", Amount: 5 }] }];

console.log(returnSpendTotals());

关于javascript - 在不改变原始数组的情况下将数组中的对象相加的算法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53872584/

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