我想对一个数组的值求和,但是我遇到了NaN错误 你能帮我吗?
const products = [
{
id: 1,
title: 'T-Shirt',
price: 98,
quantity: 10
},
{
id: 2,
title: 'Shoes',
price: 70,
quantity: 17
}
]
let cartItems = []
let parsIntPrice
let totalPrice = 0
const shoppingCart = (myId) => {
localStorage.setItem('myProduct', JSON.stringify(products))
const getStorage = localStorage.getItem('myProduct')
const finalProducts = JSON.parse(getStorage)
// SHOW EACH ITEM SELECTED
let selectedResult = finalProducts.filter(item => item.id === myId)
for (let product in selectedResult) {
console.log(selectedResult[product].title)
// CALCULATE TOTAL PRICES
parsIntPrice = selectedResult[product].price
totalPrice = cartItems.reduce((firstItem, secondItem) => firstItem + secondItem.parsIntPrice, 0)
console.log(`Total Price is: ${totalPrice}`)
}
}
答案 0 :(得分:0)
您的cartItems
数组为空,因此reduce不会对其进行迭代