In my array, I have a list of items with prices that I am attempting to sum up. Here is how it appears in the Chrome developer tools:
(3) [{…}, {…}, {…}]0: {ID: 1, ProductName: " New phone 1", Price: "€ 600"}1: {ID: 3, ProductName: " New phone 2", Price: "€ 1000"}2: {ID: 4, ProductName: " New phone 3", Price: "€ 400"}length: 3__proto__: Array(0)
I want to extract the prices for each item and calculate the total sum of all values. The number of items can vary as they are fetched from an API. Currently, this is how I am accessing the prices:
function setTotalPrice() {
fetch("http://localhost:1234/api/Product")
.then(response=>response.json())
.then(data => {
data.forEach(element => {
console.log(element.Price)
});
})
}