[英]How to Merge Matching key values in Array or Objects
What do I have wrong here?我在这里有什么问题? Please feel free to critisise and shame my question formatting, I'm trying to learn how best to ask these questions.
请随时批评和羞辱我的问题格式,我正在努力学习如何最好地提出这些问题。
Eg例如
{sellerID1: 26.52, sellerID1: 12.46, sellerID2: 72.39}
const cartItems = [{
"oq23jnq89vj3": {
sellerID: "aj8f9b73h2v37fy",
productPrice: 3.99
}
},
{
"p98nq4hn984t": {
sellerID: "aj8f9b73h2v37fy",
productPrice: 19.99
}
},
{
"vzly89nvhj08": {
sellerID: "zwlvin3u598n84w",
productPrice: 10.00
}
}
]
for (const item in cartItems) {
const sellerTotals = {}
const sellerIDs = {}
const productPrice = {}
sellerIDs.push[item.sellerID]
productPrice.push[item.productPrice]
const sellerTotals = [...SellerIDs, ...productPrice] where SellerIDs "=="
SellerIDs
}
console.log(sellerTotals)
// Desired return { sellerID1: 23.98, sellerID2: 10.00}
Thanks to the Reactiflux discord community, this was the solution to my problem of reducing
values.感谢 Reactiflux discord 社区,这是我
reducing
值问题的解决方案。
cartItems.reduce((acc, value) => {
if(acc[value.productSellerUserID]) {
acc[value.productSellerUserID] += Number(value.productPrice);
} else {
acc[value.productSellerUserID] = Number(value.productPrice);
}
return acc;
}, {}),
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.