I am currently studying Vue and creating a personal account for the gazoline company.
I have an API IMG in console log
inside the data
array there is also a transfer
array, also u can see in below picture.
This data is output like this :
I want to get the amount of income in each tankfarme
. How can I do it ?
somehow I was able to get the amount of the expense, but it receives the entire amount, and not in one tankfarm. Here is code
const getSumExpense = computed(() => {
let array = getTransfers.value.transfers.data;
let sum = 0;
array.forEach((element) => {
let arrayIncome = element.transfer;
arrayIncome.forEach((el) => {
sum = sum + el.expense;
});
});
return sum;
});
please help me, if you do not understand what I wrote, also tell me, I will try to write better
You can Array.prototype.reduce()
Code:
const arrayIncome = [{expense: 20}, {expense: 30}, {expense: 40}]
const sum = arrayIncome.reduce((a, { expense: e }) => a + e, 0)
console.log(sum)