I have a question about the Javascript mapping array with an object.
cart.json:
{ "products": [ { "id": "0.17912240212736852","qty": 1 } ], "totalPrice": 336 }
Here is my code:
let cart = { products: [], totalPrice: 0 };
const a = cart.products.map(product => {
product.id === id ? { ...product, qty: product.qty + 1 } : product;
});
// Undefined
console.log(a);
The Result I want is to add 1 to the qty
Even if I tried to do the following:
const a = cart.products.map(product => {
console.log(product);
});
// Undefined
console.log(a);
Can any explain what is the problem? Thank you.
returnto return the expression, or remove the function body{}to implicitly return.returnif you use map.forEach()with a simple+= 1would be easier (unless you really need a copy ofcart.products)