I am working on a project to get orders from an API and display them in a dashboard with some basic information (my first project). Currently what i am displaying looks like this:
Fields from left to right:
Date, Amount of products in order, price
I'm currently looping through all products in an order and display the amount, therefore it displays "1 15", the same goes for price. I want to add them up instead and just display "16".
I'm trying to create a method to do so but i cant seem to get it working. This is my current code.
HTML
<tr v-for="(order, index) in orders"
v-bind:class="index % 2 === 0 ? 'bg-white' : 'bg-gray-50'">
<td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">
{{ order.id }}
</td>
<td class="px-6 py-4 whitespace-nowrap text-sm font-medium text-gray-900">
{{ order.deliveryName }}
</td>
<td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">
{{ order.createdAt }}
</td>
<td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">
<span v-for="(product) in order.products">
{{ product.amount }}
</span>
</td>
<td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">
<span v-for="(product) in order.products">
{{ product.amount * product.price }}<br>
</span>
</td>
i tried removing the order.products loop and instead do so in a method but i couldn't get it working.
For example (i tried tweaking the method around a bit to no avail)
In HTML:
{{ getOrderAmount(order)}}
In SCRIPT:
getOrderAmount: function (order) {
let amount = 0;
order.forEach(product) in order.products
{
amount += product.amount
}
return amount;
},
How can i go about this. Help is much appreciated!
question from:
https://stackoverflow.com/questions/65901745/adding-up-a-value-from-a-nested-object 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…