【发布时间】:2020-05-20 23:27:01
【问题描述】:
在我的 mapStateToProps 中,我有一个对象(产品)数组。在每个对象中,都有一个“插件”数组,另一个对象数组。我怎样才能累积每个产品的所有“插件”的价格。
const mapStateToProps = (state) => {
return ({
...state,
totalItemsAddonPrice: ????,
totalItemPrice: state.toggleCartReducer.cartItems.reduce((accumulatedQuantity, cartItem) => accumulatedQuantity + cartItem.quantity * cartItem.price, 0),
itemCount: state.toggleCartReducer.cartItems.reduce((accumulatedQuantity, cartItem) => accumulatedQuantity + cartItem.quantity, 0),
tax: state.toggleCartReducer.cartItems.reduce((accumulatedQuantity, cartItem) => accumulatedQuantity + cartItem.quantity * cartItem.price, 0) * .0725
})}
exampleData = [
{
name:"product1",
price:10,
addons: [
{
addonName: "first add on",
addonPrice: 3
},
{
addonName: "second add on",
addonPrice: 1
},
]
},
{
name:"product2",
price:15,
addons: [
{
addonName: "product2 add on",
addonPrice: 3
},
{
addonName: "product2 second add on",
addonPrice: 2
},
]
}
]
目标是得到总价……
谢谢
【问题讨论】:
标签: javascript reactjs redux reduce