So I have an array and I want to change the total by multiplying value with qty( I changed it to english), I finally figured out how to get the total. And now I am stuck on updating the total in the right property.
I think a loop or something but it just does not makes sense to me yet on how I can make it clear it should update the right property with the right sum of value and qty.
I then later going to make it so that you can change it in the form and add to it, but that is a step further. I have been looking at Array Map and ForEach to change it. But I am getting a little stuck
The expected output would be:
{omschrijving:"Brood", value:1, qty:3, total:3},
{omschrijving:"Brocolli", value:0.99, qty:2, total:1.98},
{omschrijving:"Krentenbollen", value:1.20, qty:4, total:4.80},
{omschrijving:"Noten", value:2.99, qty:2, total:5.98}
Also I realize that my forEach outputs 5 numbers? :O
Also I removed my manual total from the property, because I wanted to change it.
Now it gives an error :) So I fill it in manual again.
let product = [
{omschrijving:"Brood", value:1, qty:3, total:},
{omschrijving:"Brocolli", value:0.99, qty:2, total:},
{omschrijving:"Krentenbollen", value:1.20, qty:4, total:},
{omschrijving:"Noten", value:2.99, qty:2, total:}
]
product.forEach(function (arrayItem) {
var x = arrayItem.value * arrayItem.qty;
console.log(x);
});
Here is a solution using simple for loop
let product = [
{omschrijving:"Brood", value:1, qty:3},
{omschrijving:"Brocolli", value:0.99, qty:2},
{omschrijving:"Krentenbollen", value:1.20, qty:4},
{omschrijving:"Noten", value:2.99, qty:2}
]
for(let i=0;i<product.length;i++)
{
product[i].total = product[i].value*product[i].qty;
}
console.log(product);
Using forEach method
let product = [
{omschrijving:"Brood", value:1, qty:3},
{omschrijving:"Brocolli", value:0.99, qty:2},
{omschrijving:"Krentenbollen", value:1.20, qty:4},
{omschrijving:"Noten", value:2.99, qty:2}
]
product.forEach(function (arrayItem) {
arrayItem.total = arrayItem.value * arrayItem.qty;
});
console.log(product);
Using .map method
let product = [
{omschrijving:"Brood", value:1, qty:3},
{omschrijving:"Brocolli", value:0.99, qty:2},
{omschrijving:"Krentenbollen", value:1.20, qty:4},
{omschrijving:"Noten", value:2.99, qty:2}
]
product.map(pro =>
{pro.total = pro.value * pro.qty;
return pro;
}
)
console.log(product);