i need to find the total price in a cart,i have to multiply the quantity that is coming from local storage with the price coming from an API
var storage = JSON.parse(localStorage.getItem("cart"));
cart.map((order_item, index) => {
return (
<li
key={index}
className="header-cart-item flex-w flex-t m-b-12"
>
<div className="header-cart-item-img">
<img src={order_item.image} alt="IMG" />
</div>
<div className="header-cart-item-txt p-t-8">
<a
href="#"
className="header-cart-item-name m-b-18 hov-cl1 trans-04"
>
{order_item.title}
</a>
<span className="header-cart-item-info">
{storage[index].Quantity} x {order_item.Price}$
</span>
</div>
</li>
);
})
i am using reactjs on the frontend and express on the backend
You can use Javascript's arithmetic operators
You can do this -
{storage[index].Quantity*order_item.Price}$
This must work.
// map cart content
const CartContent = ({ cart, storage }) => {
// You can use simple variable instead of state init
// to bypass : too many re-renders.
let cartTotal= 0
const cartList = cart.map((item, index) => {
cartTotal+=item.price*storage[index].Quantity
return (
<div>
<h2>
{item.title}
</h2>
<div>price: {item.price*storage[index].Quantity}
</div>
</div>
);
});
// render
return (
<>
{cartList}
<br/>
<p>Total {cartTotal}</p>
</>
)
}