- Forums
- vuejs
- Vuejs Calculate The Total Of The Properties Of An Object Add Object Keys
use this snippet for vuejs to add the total amount from all the object key values [5251], Last Updated: Mon Jun 24, 2024
edwin
Mon Dec 18, 2023
0 Comments
234 Visits
Goal: add all the amount property from the transactions object:
Total = 419.98
Source: /g/xampp8/htdocs/vuejs/vue-expense-tracker/src/App.vue
const transactions = ref([
{ id: 1, text: "Flower", amount: -19.99 },
{ id: 2, text: "Salary", amount: 299.97 },
{ id: 3, text: "Book", amount: -10 },
{ id: 4, text: "Camera", amount: 150 },
]);
// computed use to get the total from an object
const total = computed(()=>{
return transactions.value.reduce( (acc,transaction)=>{
return acc + transaction.amount;
}, 0)
});