Here is my code:
// get "foods" array from Firebase
const foods = querySnapshot.docs.map((doc) => ({ ...doc.data() }));
// loop in "foods" array and recover food's price
const fetchPrices = async () => {
for (const food of foods) {
const response = await fetch(`${api}/${food.id}`);
if (!response.ok) {
// throw error
}
const result = await response.json()
food.totalPrice = food.quantity * result.market_data.current_price
}
}
// run function
fetchPrices()
// this console.log show me initial values, without totalPrice key
console.log('foods : ', foods);
I assume that console.log() runs before the async/await function, but I don't know how to add this value in my initial array (or another).
What would be the way to add the key/value totalPrice to each elements of the "foods" array?