0

im new in VueJs and i have on object that includes some array in there like below and i want to show sum off all amount in one of the column table my table :

<tr v-for="(transaction,index) in transactions" >

<td>{{ index }}</td>

<td>show sum of all amount here</td

</tr>

and transaction is :

026b148e-c7dd-4891-b4d1-15a492c971a4: [
{
id: 106,
type: "income",
created_at: "2020-06-28 13:44:08",
updated_at: "2020-06-28 13:44:08",
amount: 10,
description: null,
type_of_pay: "group",
invoice_number: "026b148e-c7dd-4891-b4d1-15a492c971a4",
},
{
id: 107,
type: "income",
created_at: "2020-06-28 13:44:08",
updated_at: "2020-06-28 13:44:08",
amount: 1,
description: null,
type_of_pay: "group",
invoice_number: "026b148e-c7dd-4891-b4d1-15a492c971a4",
package: {}
}
],

how can i do what i want simple and clear?

1 Answer 1

1

Create a computed property called sum that returns the sum of all transactions and use that in the template may be?

computed: {
    sumOfTransactions() {
        return this.transactions.reduce((sum, transaction) => {
            return sum += transaction.amount;
        }, 0);
    }
}

And, use that in the template as

<td>{{ sumOfTransactions }}</td>
Sign up to request clarification or add additional context in comments.

2 Comments

thank to you but i do it before and i have this error: TypeError: this.transactions.reduce is not a function, each transaction have many object in there @prashant
@Amirali reduce is an array function, since transactions is an object you'll first need to grab the object keys Object.keys(this.transactions)...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.