I have some struggles calculating inside a for each function with JavaScript. I have one array of objects that looks like this:
[
{
"attribute_operator": "*",
"attribute_value_price": 3
},
{
"attribute_operator": "+",
"attribute_value_price": 2
},
{
"attribute_operator": "*",
"attribute_value_price": 2
}
]
As you see, every value has operator, and there is a starting price, so the real calculation should look like (if starting price is 6 for example):
6 * 3 + 2 * 2
So the output should be in that case 40.
I know how to do it for sum, but how to implement my operator easily?
tried like:
let total = starting_price.value
calculate_objects.forEach((a: { attribute_value_price: number; }) => {
total += a.attribute_value_price;
});
console.log(total);