I have a code where i must add new key. The name is total. The properties total exist equal with quantity properties where the total is property quantity multiple with property price. In my code i use spread operator to get ...product and i want add key total and input to ...product. How i add key total ?
const axios = require("axios");
const url = "";
const getRequest = async (url) => {
return (await axios.get(url)).data;
};
const result = async () => {
const carts = await getRequest("http://fakestoreapi.com/carts ");
const idProducts = await getRequest("http://fakestoreapi.com/products");
const merged = carts.map(cart => {
return {
...cart,
products: cart.products.map(product =>{
return {
...product,
//total : product.quantity * product.price,
...idProducts.find(item => product.productId === item.id)
}
})
}
})
console.log(JSON.stringify(merged,null,2));
};
result();